Physical Chemistry 3 Spring 2024, SNU
2.6 Plotting graphs
2.6.1 matplotlib module
The matplotlib module provides powerful plotting tools in Python. In this section, I won’t delve into
the details of the functions and options I used. These examples serve as references for your own plots.
As always, if you want to create something different, you can search for it online. Someone has likely
asked a similar question on the internet before. You can find the official documentation here: https:
//[Link]/stable/[Link].
Code 2.113: Matplotlib version, and setting with [Link].
1 # !pip install matplotlib
2 import matplotlib
3 import [Link] as plt
4
5 print('Current matplotlib version is:', matplotlib.__version__)
6
7 # Only if you have installed texlive in your machine
8 [Link]('text', usetex = True)
9 [Link]('font', family = 'serif')
10 [Link]('font', size = 11)
Output 2.113
Current matplotlib version is: 3.8.3
[Link] (runtime configutation) contains the default plot style for that runtime. In this
section, we specified the font type and size and enabled LATEX.
62
Physical Chemistry 3 Spring 2024, SNU
2.6.2 Line plots
Code 2.114: Trigonometric functions.
1 import numpy as np
2
3 x = [Link](0, 2 * [Link], 100)
4 y1 = [Link](x)
5 y2 = [Link](x)
6 [Link](x, y1, 'r-', linewidth = 2, label = 'sine function')
7 [Link](x, y2, 'b-', linewidth = .75, label = 'cosine function')
8 [Link](y = 0, xmin = 0, xmax = 2 * [Link], color = 'lightgray', linestyle = '--', linewidth = 1)
9 [Link](r'$x$')
10 [Link](r'$y$')
11 [Link](loc = 'lower left')
12 [Link](True, color = 'red', alpha = .6, linestyle = '-.', linewidth = .25)
13 [Link]('Trigonometric functions')
14 [Link]()
15 [Link]('[Link]')
Output 2.114
<Figure>
Figure 2.6: Output for Code 2.114.
63
Physical Chemistry 3 Spring 2024, SNU
2.6.3 Scatter plots
Code 2.115: Scatter plot example.
1 x = [Link](1000)
2 y = [Link](1000)
3 colors = [Link](1000)
4
5 [Link](x, y, c = colors, s = 10, alpha = .5, cmap = 'jet')
6 [Link]('Scatter plot')
7 [Link]()
8 [Link]()
Output 2.115
<Figure>
Figure 2.7: Output for Code 2.115.
64
Physical Chemistry 3 Spring 2024, SNU
2.6.4 Inserting error bars
Code 2.116: Error bar example.
1 x = [Link](0, 4, 25)
2 y = -x ** 2 + 4 * x
3 yerr = [Link](25)
4
5 [Link](x, y, yerr, alpha = .5, fmt = 'k.', capsize = 3, capthick = .5)
6 [Link](x, y, s = 25, marker = 'x', alpha = .75)
7 [Link]('Scatter plot with errorbars')
8 [Link]()
Output 2.116
<Figure>
Figure 2.8: Output for Code 2.116.
65
Physical Chemistry 3 Spring 2024, SNU
2.6.5 3D plots
Code 2.117: 3D surface plot example.
1 from mpl_toolkits.mplot3d import axes3d
2 import [Link] as plt
3
4 X, Y = [Link](0, 1, 1000), [Link](0, 1, 1000)
5 XX, YY = [Link](X, Y)
6 sigma = 1.
7 ZZ = [Link](- ([Link]((XX ** 2 + YY ** 2)) - 0.5) ** 2 / (2 * (sigma ** 2))) / [Link](2. * [Link] * (
sigma ** 2))
8
9 fig = [Link](figsize = (12, 10))
10 ax = fig.add_subplot(projection='3d')
11 ax.set_xlabel(r'$X$')
12 ax.set_ylabel(r'$Y$')
13 ax.set_zlabel(r'$Z$')
14 ax.set_title('3D plot example')
15 surface = ax.plot_surface(XX, YY, ZZ, cmap = plt.get_cmap('viridis'))
16 [Link](surface, ax = ax, shrink = 0.5)
17
18 [Link]()
Output 2.117
<Figure>
Figure 2.9: Output for Code 2.117.
66
Physical Chemistry 3 Spring 2024, SNU
2.6.6 Subplots
Code 2.118: Subplots example.
1 fig, axes = [Link](2, 4)
2 fig.set_size_inches(12, 4)
3 plt.subplots_adjust(wspace = 0.3, hspace = 0.3)
4
5 x = [Link](0, [Link], 1000)
6
7 for k, idx in zip(range(0,8), range(1, 9)):
8 t = k * [Link] / 4
9 q = [Link](x) * [Link](t)
10 [Link](2, 4, idx)
11 [Link](0, [Link])
12 [Link](-1.2, 1.2)
13 [Link](x, q, 'k-', label=k)
14 [Link](loc='upper right')
15
16 [Link]()
Output 2.118
<Figure>
Figure 2.10: Output for Code 2.118.
67
Physical Chemistry 3 Spring 2024, SNU
2.6.7 Inserting colorbars
Code 2.119: Colorbar example.
1 from [Link] import ScalarMappable
2
3 cmap = 'jet'
4 x = [Link](0, 2 * [Link], 300)
5
6 def translated_sine(x, t):
7 return [Link](x - t)
8
9 fig, ax = [Link](figsize = (8, 6))
10
11 for i, t in enumerate([Link](0, [Link], 20)):
12 [Link](x, translated_sine(x, t), linewidth = 1,
13 color = plt.get_cmap(cmap)(i / 20))
14
15 norm = [Link](0, [Link])
16 sm = ScalarMappable(cmap = cmap, norm = norm)
17 sm.set_array([])
18 cbar = [Link](sm, ax = ax, label = 'Translation')
19
20 [Link](r'$x$')
21 [Link](r'$y$')
22 [Link](-1.5, 1.5)
23 [Link](r'$y = \sin(x-t),\;t\in [0,\pi]$')
24 [Link]()
Output 2.119
<Figure>
Figure 2.11: Output for Code 2.119.
68