Matplotlib usage notes (1) getting started usage

The first point we need to know is that matplotlib is a library for drawing statistical graphs in python, which provides many methods of drawing.

I won’t talk about the installation of matplotlib

Let's try a piece of code directly

from matplotlib import pyplot as plt #导入包并重命名
x=range(2,26,2) #创建横左边
y=[12,34,23,45,67,43,87,45,67,12,90,55] #创建纵坐标
plt.plot(x,y) # 画图
plt.show() #将图显示出来

The image produced by this piece of code is as follows (a bit ugly, but innocuous):
pyplot_01

This is the simplest drawing code, but it is far from enough for us who are pursuing beauty. We probably need the following questions about the ending:

The code corresponding to the above problem is given below (adding step by step to the original code):

  • Resize the picture
from matplotlib import pyplot as plt #导入包并重命名
plt.figure(figsize=(20,8),dpi=80)
# (20.8)宽和高,dpi像素点密集度
x=range(2,26,2) #创建横左边
y=[12,34,23,45,67,43,87,45,67,12,90,55] #创建纵坐标
plt.plot(x,y) # 画图
plt.show() #将图显示出来

The picture output is as follows:
pyplot_02

How specific figsize and dpi affect the final image size, you can try more by yourself.

  • Save the picture locally
#只需要在画完图之后加一句
plt.savefig("path\filename.png")
# path为图片保存的路径,建议使用相对路径

The effect is nothing to show, but there are more files in the folder

  • Adjust scale
from matplotlib import pyplot as plt #导入包并重命名
plt.figure(figsize=(20,8),dpi=80)
# (20.8)宽和高,dpi像素点密集度
x=range(2,26,2) #创建横左边
y=[12,34,23,45,67,43,87,45,67,12,90,55] #创建纵坐标
plt.xticks([1,2,3,6,7,18,19])
plt.plot(x,y) # 画图
plt.show() #将图显示出来

The effect is shown in the figure below, which means that once the corresponding relationship between the x and y axes is determined, the scale value of my abscissa can be specified arbitrarily.
pyplot_03
By analogy, the scale of the y-axis is

plt.yticks([1,2,3,4,5]) 
  • Describe the x, y axis (including name and unit)
from matplotlib import pyplot as plt  # 导入包并重命名
plt.figure(figsize=(20, 8), dpi=100)
# (20.8)宽和高,dpi像素点密集度
x = range(2, 26, 2)  # 创建横左边
y = [12, 34, 23, 45, 67, 43, 87, 45, 67, 12, 90, 55]  # 创建纵坐标
plt.xticks([1,2,3.5,6,7,18,19])
plt.xlabel("time")
plt.ylabel("temp")
plt.title("temp-time")
plt.plot(x, y)  # 画图
plt.show()  # 将图显示出来

The display result is as follows:
pyplot_04
There will be a problem here, the added text cannot be Chinese, we need to set this up, refer to the blog

  • Line attributes

The line attribute is whether the style of the image line is thick or thin, solid line or dashed line, we can add the line attribute parameter to the plot method to set it, for the convenience of comparison, here we set two curves, that is, use plt.plot twice. ()

from matplotlib import pyplot as plt  # 导入包并重命名
plt.figure(figsize=(20, 8), dpi=100)
# (20.8)宽和高,dpi像素点密集度
x = range(2, 26, 2)  # 创建横左边
y_1= [12, 34, 23, 45, 67, 43, 87, 45, 67, 12, 90, 55]  # 创建纵坐标
y_2= [11, 36, 20, 40, 60, 49, 80, 49, 69, 10, 90, 55]
plt.xticks([1,2,3.5,6,7,18,19])
plt.xlabel("time")
plt.ylabel("temp")
plt.title("temp-time")
plt.plot(x, y_1,label="today",color="red",linestyle="--")  # 画图
plt.plot(x, y_1,label="yesterday",color="blue",linestyle=":")  
plt.grid()
plt.legend() #添加图例
plt.show()  # 将图显示出来

The renderings are as follows: as a
pyplot_05
reminder, plt.legend() can change the position by adding parameters, see the blog for details

  • Draw a scatter plot

Drawing a scatter plot is actually very simple, just replace plt.plot() with plt.scatter()

from matplotlib import pyplot as plt  # 导入包并重命名
plt.figure(figsize=(20, 8), dpi=100)
# (20.8)宽和高,dpi像素点密集度
x = range(2, 26, 2)  # 创建横左边
y_1= [12, 34, 23, 45, 67, 43, 87, 45, 67, 12, 90, 55]  # 创建纵坐标
y_2= [11, 36, 20, 40, 60, 49, 80, 49, 69, 10, 90, 55]
plt.xticks([1,2,3.5,6,7,18,19])
plt.xlabel("time")
plt.ylabel("temp")
plt.title("temp-time")
plt.scatter(x, y_1,label="today",color="red",linestyle="--")  # 画图
plt.scatter(x, y_2,label="yesterday",color="blue",linestyle=":")  
plt.grid()
plt.legend() #添加图例
plt.show()  # 将图显示出来

The effect is as follows:
pyplot_06

  • Draw bar graph

It is also very simple to draw a bar graph. Replace plt.plot() with plot.bar(). For the sake of beauty, we will change the data of the graph

from matplotlib import pyplot as plt  # 导入包并重命名
plt.figure(figsize=(20, 8), dpi=100)
# (20.8)宽和高,dpi像素点密集度
x = range(1,7) # 创建横左边
y = [3,5,6,4,3,2]
plt.xlabel("time")
plt.ylabel("temp")
plt.title("temp-time")
plt.bar(x, y,label="today",color="red",width=0.3)  # 画图
plt.grid()
plt.legend() #添加图例
plt.show()  # 将图显示出来

The display results are as follows:
pyplot_07

If we want to talk to a histogram, we need to perform the following operations. Simply put, stagger the abscissa by one unit of the column width, otherwise it will overlap.

from matplotlib import pyplot as plt  # 导入包并重命名
plt.figure(figsize=(20, 8), dpi=100)
# (20.8)宽和高,dpi像素点密集度
x = range(1, 7)  # 创建横左边
y_1 = [3, 5, 6, 4, 3, 2]
y_2 = [4, 1, 3, 6, 8, 9]
y_3 = [10, 2, 4, 7, 10, 8]
plt.xlabel("time")
plt.ylabel("temp")
plt.title("temp-time")
bar_width = 0.2 #柱宽
x_2 = [i + bar_width for i in x]#错开
x_3=[i + 2*bar_width for i in x]
plt.bar(x, y_1, label="today", color="red", width=bar_width)  # 画图
plt.bar(x_2, y_2, label="tomorrow", color="green", width=bar_width)  # 画图
plt.bar(x_3, y_3, label="yesterday", color="gray", width=bar_width)  # 画图
plt.grid()
plt.legend()  # 添加图例
plt.show()  # 将图显示出来

The renderings are as follows:
pyplot_08

  • Other statistical graphs

For pyplot, mastering the above statistical graph is far from enough, so the matplotlib official website provides us with many templates, just download the template and modify it.

Guess you like

Origin blog.csdn.net/qq_41459262/article/details/104522693