首页 > 其他 > 详细

matplotlib的基本用法

时间:2020-08-13 12:20:09      阅读:52      评论:0      收藏:0      [点我收藏+]

matplotlib.pyplot

 

1.画柱状图

matplotlib.pyplot.bar(x, height, width=0.8,bottom=None)

在画堆叠柱状图的时候可以设置bottom

import matplotlib.pyplot as plt
import numpy as np

labels = [A,B,C,D,E]
x1 = [20,15,30,25,18]
x2 = [23,30,20,12,10]
lps = np.arange(len(labels))  # 设置刻度标签的位置
width = 0.35

# fig = plt.figure(figsize=(6,5))
# ax = fig.add_subplot()  # 与下面的等价
fig, ax = plt.subplots(figsize=(6,5))  # fig表示绘图窗口,ax表示窗口里的坐标系,可以创建多个子图
# ax1,ax2 = ax.ravel() # 有多个子图时,获得每个子图的句柄
# 显示柱状高度
for i,j in zip(lps, x1):
    plt.text(i-width/2, j+0.005, j, ha=center, va=bottom)
for i,j in zip(lps, x2):
    plt.text(i+width/2, j+0.005, j, ha=center, va=bottom)

ax.bar(lps-width/2, x1, width=width, label=G1)
ax.bar(lps+width/2, x2, width=width, label=G2)
ax.set_xticks(lps)          # 设置柱状图的标签位置
ax.set_xticklabels(labels)  # 设置柱状图的标签,与上式联用
ax.set_ylabel(scores)
ax.set_title(scores of G1 and G2)
ax.legend()

fig.tight_layout()
plt.show()

‘‘‘
# 画多条并列的柱状图
ax.bar(lps, x1, width=width, label=‘G1‘)
ax.bar(lps+width, x2, width=width, label=‘G2‘)
ax.bar(lps+2*width, x3, width=width, label=‘G3‘)
ax.set_xticks(lps+width) 
ax.set_xticklabels(labels)  
‘‘‘

 

技术分享图片

 

2.画直方图

plt.hist(x, bins=None, density=None)

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm

mu,sigma = 0, 1
x = mu + sigma*np.random.randn(10000)

n, bins, patches = plt.hist(x, bins=50, density=True, facecolor=b, alpha=0.35)
y = norm.pdf(bins, mu, sigma)  # 画概率分布曲线
plt.plot(bins, y, r--)
plt.ylabel(Probability)
plt.text(-3, .4, r$\mu=0,\ \sigma=1$)
plt.grid()
plt.show()

 

技术分享图片

 

3.画饼状图

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randint(10, 100, 6)
label = list(abcdef)
explode = (0.05,0,0,0,0,0)  # 突出显示第一块
plt.pie(x, labels=label, autopct=%1.1f%%, explode=explode)
# bbox_to_anchor 定位图例的框
plt.legend(loc="center left", bbox_to_anchor=(1, 0, 0.5, 1))
plt.show()

技术分享图片

 

matplotlib的基本用法

原文:https://www.cnblogs.com/pineapple-chicken/p/12326069.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!