07 Python Matplotlib library to draw pie charts and histograms

Draw a pie chart

# 创建 石头,剪刀,布 的出现频率

s = np.random.randint(4000,9000)

j = np.random.randint(4000,9000)

b = np.random.randint(4000,9000)

            
s_perc = s/(s+j+b)
j_perc = j/(s+j+b)
b_perc = b/(s+j+b)    

laverls = ['锤子', '剪子', '布']
plt.rcParams['font.sans-serif'] = ['SimHei']  # 正常显示中文标签

colors = ['blue','red', 'green']

## labels 名称; explode 分裂大小 autopct 百分比显示
paches, texts, autotexts = plt.pie([s_perc, j_perc, b_perc], labels = laverls, colors = colors, explode = (0.05, 0.05,0.05), autopct = '%0.1f%%')

for text in autotexts:
  text.set_color('white')

for text in texts + autotexts:
    text.set_fontsize(20)


    
plt.show()

Output result:

Insert picture description here
It's ugly ~~~~~

Draw a histogram

x = np.random.randn(1000)
##plt.hist(x)
plt.hist(x, bins = 100) # 利用bins 来修改柱的宽度
plt.show()

Output result

Insert picture description here

#使用np.random.normal()指定期望和均值的正太分布
x = np.random.normal(0, 0.8, 1000)
y = np.random.normal(-2, 1, 1000)
z = np.random.normal(3, 2, 1000)
kwargs = dict(bins = 100, alpha = 0.5)
plt.hist(x,**kwargs)
plt.hist(y,**kwargs)
plt.hist(z,**kwargs)
plt.show()

Output result:

Insert picture description here

Published 36 original articles · praised 0 · visits 624

Guess you like

Origin blog.csdn.net/Corollary/article/details/105401408