Wu Yuxiong - born natural python learning Notes: Matplotlib basic drawing

When using Matplotlib assembly drawing, often with the use of the Numpy components. 
Use Matplotlib first introduced Matplotlib drawing assembly, since most of the mapping function in 
matplotlib. Pyplot in it will usually introduced matplotlib. Pyplot provided a brief alias for 
easy input. For example, we can take the alias for the pit:

 

 

The main function is to Matplotlib made to FIG x, y coordinates in FIG. When drawing, we need to x, y coordinates 
stored in the variable list and passed Matplotlib. For example, we give the system six points:

 

 

The number of elements in the list x coordinate and y coordinates of the list must be the same, or will cause "x execution and y 
error must have same first dimen s ion ,, of.
. Matplotlib pyplot method of drawing lines of FIG plot, the syntax is:

 

 

For example, we use a list of stx li and Ii sty are plotted:

 

 

If the drawing is not displayed automatically, it s how the method can be used, for example:

 

 

import matplotlib.pyplot as plt

listx = [1,5,7,9,13,16]
listy = [15,50,80,40,70,50]
plt.plot(listx, listy, color ="red")
plt.show()

 

 

and parameter setting method for graphic plot 
plot matplotlib.pyplot method, in addition to the x-coordinate and y-coordinate list is a list of the required parameters, 
there are dozens of optional parameters can be used to set the characteristics of the drawing, the following are four common optional parameters :

 

 

 

 

For example, a red system, a broken line, the line width is 5, the pattern name is a line graph of food:

 

 

After setting the properties, methods of implementation of legend display:

 

 

At the same time draw multiple graphics 
in the same coordinate system can draw multiple graphs, we will usually drawn after the completion of all the graphics are 
displayed. For example, we give the system two graphics:

 

 

If you do not set the line color, the system will automatically set a different color.
Imprison shape disposed 
after the graph is plotted, some settings may be done, as the title, x and y coordinate axes of the graphic title or the like, so that 
allow more intuitive graphical look. 
Set the graph title, x axis titles, y axis titles syntax are:

 

 

如果没有指定 x 坐标及 y 坐标范围,系统会根据数据判断最适合的 x 坐标及 y
坐标范围。我们也可以自行设置 x 、 y 的坐标范围,语法为 :

 

 

绘制两个线形图并设置其各个图形属性。

 

 

import matplotlib.pyplot as plt

listx1 = [1,5,7,9,13,16]
listy1 = [15,50,80,40,70,50]
plt.plot(listx1, listy1, label="Male")
listx2 = [2,6,8,11,14,16]
listy2 = [10,40,30,50,80,60]
plt.plot(listx2, listy2, color="red", linewidth=5.0, linestyle="--", label="Female")
plt.legend()
plt.xlim(0, 20)
plt.ylim(0, 100)
plt.title("Pocket Money")
plt.xlabel("Age")
plt.ylabel("Money")
plt.show()

 

 

在 Matplotlib 中显示中文
Matplotlib 默认无法显示中文,所以在前面的例子中各种标题及图例使用的都是
英文。若想在 Matplotlib 显示中文,只需将其默认使用的字体更改为简体中文即可。
更换字体的操作方法有两种:

 

 

 

 在自己python的安装路径中找到:

 

 

 

 

 

 这种设置是永久性的。修改后建议把集成开发工具重启加载一下。

以上操作可能还存在一些问题,更为详细的的做法请参考这个链接:https://www.cnblogs.com/tszr/p/11228013.html

绘制柱状图及饼图
Matplotlib 除了可绘制线图外,还可绘制柱状图或饼图。
柱状图的绘制是通过 bar 方法来实现的,其语法为 :

 

 

绘制柱状图的参数与绘制线形图类似,除了一些线 的 属性参数(如线宽、线样
式等〉不能使用外,其余参数在绘制柱状图时都可以使用。
下例与前面的例子相同 , 只是现在我们用柱状图来呈现, 并用中文显示各项文字。

Guess you like

Origin www.cnblogs.com/tszr/p/12028256.html