[Python library] matplotlib
The most commonly used in the matplotlib library is pyplot. To display the image you want, you must first configure the image, and then display the image.
Image configuration:
plt.plot(……)
plt.……
Image display (it will be called every time the image is displayed later, which is omitted in this blog, and plt.show() needs to be added when running by itself):
plt.show()
Input: It can be an ndarray in numpy, a list, or a Dataframe.
xpoints = np.array([0, 1, 2, 6, 14, 55, 123, 545])
ypoints = np.array([0, 6, 100, 250, 232, 565, 32, 45])
y1 = np.array([1, 544, 12, 43, 24, 345, 343, 657])
y2 = np.array([56, 325, 3, 78, 34, 2, 5234, 42])
By default, a line is drawn from point to point:
plt.plot(xpoints, ypoints)
Wireless drawing:
plt.plot(xpoints, ypoints, 'o')
marker mark:
plt.plot(xpoints, ypoints, marker='o')
plt.plot(xpoints, ypoints, marker='*')
Circle Marker | Dashed Line | Red, PointSize = 20:
plt.plot(xpoints, ypoints, 'o:r', ms=20)
Lines, half of them are represented by linestyle or ls, and linewidth is represented by lw:
plt.plot(xpoints, ypoints, linestyle='dotted')
plt.plot(xpoints, y1, ls='--')
Create labels, titles for plots:
# 字体
font1 = {
'family':'serif', 'color':'blue', 'size':20}
plt.title("This is Title", fontdict=font1)
plt.xlabel('x')
plt.ylabel('y')
plt.plot(xpoints, y1, 'o-g')
Display multiple plots:
# 表示一行两列的第一张子图
plt.subplot(1, 2, 1)
plt.plot(xpoints, y1)
plt.title("graph1")
# 表示一行两列的第二张子图
plt.subplot(1, 2, 2)
plt.plot(xpoints, y2)
plt.title("graph2")
# 超级标题
plt.suptitle("graph")
Scatterplot:
plt.scatter(xpoints, y1, color='hotpink')
plt.scatter(xpoints, y2, color="green")
Histogram:
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
# 柱状图
plt.bar(x, y)
plt.subplot(1, 2, 2)
# 水平柱状图
plt.barh(x, y, color='green')
Histogram:
x = np.random.normal(170, 10, 250)
print(x)
plt.hist(x)