Python study notes for the 65th day (Matplotlib draws multiple graphs)

Python study notes for the sixty-fifth day

Matplotlib draws multiple figures

We can use the subplot() and subplots() methods in pyplot to draw multiple subplots.

The subplot() method needs to specify the position when drawing, and the subplots() method can generate multiple ones at a time, and only needs to call the ax of the generated object when calling.

subplot()

subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(**kwargs)
subplot(ax)

The above function divides the entire drawing area into nrows rows and ncols columns, and then numbers each sub-area from left to right and from top to bottom 1...N, the upper left sub-area is numbered 1, and the lower right area is numbered N, the number can be set through the parameter index.

Setting numRows = 1 and numCols = 2 means drawing the chart into a 1x2 picture area. The corresponding coordinates are:

(1, 1), (1, 2)
plotNum = 1, the coordinates represented are (1, 1), that is, the subgraph of the first row and first column.

plotNum = 2, the coordinates represented are (1, 2), that is, the subplot in the first row and second column.

# 实例 1
import matplotlib.pyplot as plt
import numpy as np

#plot 1:
xpoints = np.array([0, 6])
ypoints = np.array([0, 100])

plt.subplot(1, 2, 1)
plt.plot(xpoints,ypoints)
plt.title("plot 1")

#plot 2:
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])

plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("plot 2")

plt.suptitle("RUNOOB subplot Test")
plt.show()

Setting numRows = 2 and numCols = 2 means drawing the chart into a 2x2 picture area. The corresponding coordinates are:

(1, 1), (1, 2)
(2, 1), (2, 2)
plotNum = 1, the coordinates represented are (1, 1), that is, the subgraph of the first row and first column.

plotNum = 2, the coordinates represented are (1, 2), that is, the subplot in the first row and second column.

plotNum = 3, the coordinates represented are (2, 1), which is the subgraph of the second row and first column.

plotNum = 4, the coordinates represented are (2, 2), that is, the subplot in the second row and second column.

# 实例 2
import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x = np.array([0, 6])
y = np.array([0, 100])

plt.subplot(2, 2, 1)
plt.plot(x,y)
plt.title("plot 1")

#plot 2:
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])

plt.subplot(2, 2, 2)
plt.plot(x,y)
plt.title("plot 2")

#plot 3:
x = np.array([1, 2, 3, 4])
y = np.array([3, 5, 7, 9])

plt.subplot(2, 2, 3)
plt.plot(x,y)
plt.title("plot 3")

#plot 4:
x = np.array([1, 2, 3, 4])
y = np.array([4, 5, 6, 7])

plt.subplot(2, 2, 4)
plt.plot(x,y)
plt.title("plot 4")

plt.suptitle("RUNOOB subplot Test")
plt.show()

subplots()

The syntax format of the subplots() method is as follows:

matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)

Parameter Description:

  • nrows: Default is 1, set the number of rows of the chart.
  • ncols: Default is 1, set the number of columns of the chart.
  • sharex, sharey: Set whether the x and y axes share attributes, the default is false, and can be set to 'none', 'all', 'row' or 'col'. False or none each subplot's x or y axis is independent, True or 'all': all subplots share an x ​​or y axis, 'row' sets each subplot row to share an x ​​or y axis,' col': Set each subgraph column to share an x-axis or y-axis.
  • squeeze: Boolean value, the default is True, indicating that extra dimensions are squeezed out of the returned Axes (axes) object, for N 1 or 1 N subplots, a 1-dimensional array is returned, for N*M, N>1 and M >1 returns a 2-dimensional array. If set to False, no extrusion is performed and a 2D array of Axes instances is returned, even if it ends up being 1x1.
  • subplot_kw: optional, dictionary type. Pass the dictionary keys to add_subplot() to create each subplot.
  • gridspec_kw: optional, dictionary type. Pass the dictionary keys to the GridSpec constructor to create the subgraph and place it in the grid.
  • **fig_kw: Pass detailed keyword arguments to the figure() function.
# 实例 3
import matplotlib.pyplot as plt
import numpy as np

# 创建一些测试数据 -- 图1
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)

# 创建一个画像和子图 -- 图2
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')

# 创建两个子图 -- 图3
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)

# 创建四个子图 -- 图4
fig, axs = plt.subplots(2, 2, subplot_kw=dict(projection="polar"))
axs[0, 0].plot(x, y)
axs[1, 1].scatter(x, y)

# 共享 x 轴
plt.subplots(2, 2, sharex='col')

# 共享 y 轴
plt.subplots(2, 2, sharey='row')

# 共享 x 轴和 y 轴
plt.subplots(2, 2, sharex='all', sharey='all')

# 这个也是共享 x 轴和 y 轴
plt.subplots(2, 2, sharex=True, sharey=True)

# 创建标识为 10 的图,已经存在的则删除
fig, ax = plt.subplots(num=10, clear=True)

plt.show()

postscript

What you are learning today is how to draw multiple graphs using Python Matplotlib. A summary of today’s learning content:

  1. Matplotlib draws multiple figures
  2. subplot()
  3. subplots()

Guess you like

Origin blog.csdn.net/qq_54129105/article/details/132394378