Examples of Matplotlib basic drawing functions: pie chart, histogram, polar graph, scatter plot

Insert picture description here
Insert picture description here
Insert picture description here

Pyplot pie chart drawing

Code:

import matplotlib.pyplot as plt

labels = 'Frogs','Hogs','Dogs','Logs'  #各部分的标签
sizes = [15,30,45,10]   #各部分的大小,共100
explode = (0,0.1,0,0)   #Hogs分离的距离

plt.pie(sizes,explode = explode,labels = labels,autopct = '%1.1f%%',\
        shadow = False,startangle = 90)

plt.axis('equal')    #使饼图成为一个正圆
plt.show()

Result graph:
Insert picture description here

Pyplot histogram drawing

Code:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)   #生成随机数,0为种子
mu,sigma = 100,20   #均值和标准差
a = np.random.normal(mu,sigma,size=100)

plt.hist(a,20, normed=1, histtype = 'stepfilled', facecolor='b',alpha=0.75)
#bin:20是直方图的个数
#normed : boolean, optional(?)是否将得到的直方图向量归一化
#histtype : {‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’},optional(选择展示的类型,默认为bar)
#alpha:透明度,0为全透明,1为不透明
plt.title('Histogram')

plt.show()

For specific pyplot.hist function parameters, please refer to:
https://blog.csdn.net/lmj_2529980619/article/details/86063752

Pyplot polar plot drawing

Insert picture description here
Code:

import numpy as np
import matplotlib.pyplot as plt

N = 20
theta = np.linspace(0.0,2*np.pi,N,endpoint=False)
radii = 10 * np.random.rand(N)
width = np.pi / 4* np.random.rand(N)

ax = plt.subplot(111,projection='polar')
bars = ax.bar(theta, radii,width=width,bottom=0.0)

for r,bar in zip(radii,bars):
    bar.set_facecolor(plt.cm.viridis(r / 10))
    bar.set_alpha(0.5)

plt.show()

Result graph:
Insert picture description here

Pyplot scatter plot drawing

import numpy as np
import matplotlib.pyplot as plt

fig,ax = plt.subplots()
ax.plot(10*np.random.randn(100),10*np.random.randn(100),'o')
ax.set_title('Simple Scatter')

plt.show()

Result graph:
Insert picture description here
dynamic drawing of polar coordinates graph:
https://blog.csdn.net/for_dream1205/article/details/98953843?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase&depth_1-utm_source=distribute.pc_relevant .none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase

Guess you like

Origin blog.csdn.net/langezuibang/article/details/106137471