当前位置:网站首页>Preparation Notes: Matplotlib learning notes a

Preparation Notes: Matplotlib learning notes a

2022-07-19 12:32:00 Raine_ Yang

1 establish figure Object to start drawing

import matplotlib.pyplot as plt
fig = plt.figure()

2 Create an axis as the drawing base

ax = fig.add_subplot(111)
ax.set(xlim=[0.5, 4.5], ylim=[-2, 8], title='An Example Axes',
       ylabel='Y-Axis', xlabel='X-Axis')
plt.show()

fig.add_subplot() It refers to dividing the canvas into several equal parts .111 For the entire canvas

ax.set In the function xlim by x Upper and lower limits of coordinates ,ylim by y Upper and lower limits of coordinates ,title Is the chart name ,ylabel,xlable by y Axis and x The name of the shaft

 Insert picture description here

fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)

 Insert picture description here

Divide the canvas in half, and you can draw 4 Map , The third parameter corresponds to the canvas position

Create multiple axes at once and save them to a two-dimensional array

fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].set(title='Upper Left')
axes[0,1].set(title='Upper Right')
axes[1,0].set(title='Lower Left')
axes[1,1].set(title='Lower Right')

Use plot Function line , The first two parameters correspond to x,y Axis data , You can add colors later , Line type and other parameters

x = np.linspace(0, np.pi)
y_sin = np.sin(x)
y_cos = np.cos(x)

ax1.plot(x, y_sin)
ax2.plot(x, y_sin, 'go--', linewidth=2, markersize=12)
ax3.plot(x, y_cos, color='red', marker='+', linestyle='dashed')

 Insert picture description here

fig.clear()

Clear the canvas , Clear all axes and images

The string key drawing of the incoming dictionary :

x = np.linspace(0, 10, 200)
data_obj = {
    'x': x,
            'y1': 2 * x + 1,
            'y2': 3 * x + 1.2,
            'mean': 0.5 * x * np.cos(2*x) + 2.5 * x + 1.1}

# Plot the "centerline" with `plot`
ax.plot('x', 'mean', color='black', data=data_obj)

plt.show()

Here, the string is passed in the data part , These strings correspond to data_obj The key inside , Pass in the dictionary 、data_obj As data It can automatically read the value corresponding to the key for drawing
 Insert picture description here
matplotlib And the scatter chart , Bar chart , Histogram , The pie chart , Box diagram , Bubble chart , Contour and other options , See article https://blog.csdn.net/qq_34859482/article/details/80617391

原网站

版权声明
本文为[Raine_ Yang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207171644303021.html