python库之matplotlib
使用场景
大多数Matplotlib实用程序都位于pyplot子模块下,通常以plt别名导入
通过该库我们可以很容易的绘制出折线图、直方图、散点图、饼图等丰富的统计图,安装使用 pip install matplotlib 命令即可,Matplotlib 经常会与numpy 一起使用。
1. 折线图
from matplotlib import pyplot as plt
x = range(1, 7)
'''
figsize:设置图片的宽、高,单位为英寸
dpi:设置分辨率
color:颜色
linewidth:线的宽度
marker:折点样式
linestyle:线的样式,主要包括:'-'、'--'、'-.'、':'
marker='o'
'''
plt.figure(figsize=(8, 5), dpi=80)
y = [13, 15, 14, 16, 15, 17]
plt.title('折线图')
plt.xlabel('x 轴')
plt.ylabel('y 轴')
plt.plot(x, y, color='red',marker='o', linewidth=1, linestyle='--')
plt.show()
# 保存# plt.savefig('test.png')
from matplotlib import pyplot as plt
x = range(15, 25)
y1 = [50, 55, 58, 65, 70, 68, 70, 72, 75, 70]
y2 = [52, 53, 60, 63, 65, 68, 75, 80, 85, 72]
plt.figure(figsize=(10, 6), dpi=80)
plt.title('体重年龄折线图')
plt.xlabel('年龄(岁)')
plt.ylabel('体重(kg)')
plt.plot(x, y1, color='red', label='张三')
plt.plot(x, y2, color='blue', label='李四')
# 添加网格,alpha 为透明度
plt.grid(alpha=0.5)
# 添加图例
plt.legend(loc='upper right')
plt.show()
2. 子图
from matplotlib import pyplot as plt
import numpy as np
a = np.arange(1, 30)
# 划分子图
fig, axs = plt.subplots(2, 2)
# 绘制子图
axs1 = axs[0, 0]
axs2 = axs[0, 1]
axs3 = axs[1, 0]
axs4 = axs[1, 1]
axs1.plot(a, a)
axs2.plot(a, np.sin(a))
axs3.plot(a, np.log(a))
axs4.plot(a, a ** 2)
plt.show()
3. 散点图
from matplotlib import pyplot as plt
import numpy as np
x = np.arange(0, 20)
# 生成随机数
y = np.random.randint(0, 20, size=20)
plt.title('散点图')
plt.xlabel('x 轴')
plt.ylabel('y 轴')
plt.plot(x, y, 'ob')
plt.show()
4. 直方图
from matplotlib import pyplot as plt
import numpy as np
# 生成随机数
d1 = np.random.randn(5000)
d2 = np.random.randn(4000)
'''
bins:直方图条目数
alpha:透明度
label:图例名
'''
plt.hist(d1, bins=50, label = 'label1', alpha=0.8)
plt.hist(d2, bins=50, label = 'label2', alpha=0.5)
plt.grid(alpha=0.3)
plt.title('直方图')
plt.xlabel('x 轴')
plt.ylabel('y 轴')
# 显示图例
plt.legend()
plt.show()
5. 条形图
from matplotlib import pyplot as plt
import numpy as np
arr = np.arange(4)
x = ['张三', '李四', '王五', '赵六']
y = [77, 79, 70, 70]
'''
width:长条形宽度
label:图例名
'''
rects = plt.bar(arr, y, width=0.3, label='语文')
'''
参数1:中点坐标
参数2:显示值
'''
plt.xticks([idx for idx in range(len(x))], x)
plt.title('学生成绩条形图')
plt.xlabel('姓名')
plt.ylabel('成绩')
plt.legend()
# 在条形图上加标注
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2, height, str(height), ha='center', va='bottom')
plt.show()
6. 横置
from matplotlib import pyplot as plt
import numpy as np
arr = np.arange(4)
y = ['张三', '李四', '王五', '赵六']
x = [88, 79, 70, 66]
plt.barh(range(4), x, 0.4, label='语文')
plt.yticks(range(4), y)
plt.xlabel('成绩')
plt.ylabel('姓名')
plt.title('学生成绩条形图')
plt.legend(loc='upper right')
for x, y in enumerate(x):
plt.text(y + 0.2, x - 0.1, '%s' % y)
plt.show()
from matplotlib import pyplot as plt
import numpy as np
arr = np.arange(4)
x = ['张三', '李四', '王五', '赵六']
y1 = [88, 75, 77, 66]
y2 = [77, 79, 70, 70]
'''
width:长条形宽度
label:图例名
'''
rects1 = plt.bar(arr, y1, width=0.3, label='语文')
rects2 = plt.bar(arr + 0.3, y2, width=0.3, label='数学')
'''
参数1:中点坐标
参数2:显示值
参数3:间距
'''
plt.xticks([idx + 0.15 for idx in range(len(x))], x, rotation=10)
plt.title('学生成绩条形图')
plt.xlabel('姓名')
plt.ylabel('成绩')
plt.legend()
# 编辑文本
for rect in rects1:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2, height, str(height), ha='center', va='bottom')
for rect in rects2:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2, height, str(height), ha='center', va='bottom')
plt.show()
7. 饼图
import matplotlib.pyplot as plt
label_list = ['第一部分', '第二部分', '第三部分']
size = [50, 30, 20]
# 各部分颜色
color = ['red', 'green', 'blue']
# 各部分突出值
explode = [0, 0.1, 0]
'''
explode:设置各部分突出
label:设置图例显示内容
labeldistance:设置图例内容距圆心位置
autopct:设置圆里面文本
shadow:设置是否有阴影
startangle:起始角度,默认从 0 开始逆时针转
pctdistance:设置圆内文本距圆心距离
l_text:圆内部文本
p_text:圆外部文本
'''
patches, l_text, p_text = plt.pie(size, explode=explode, colors=color, labels=label_list, labeldistance=1.1, autopct="%1.1f%%", shadow=False, startangle=90, pctdistance=0.6)
# 设置横轴和纵轴大小相等,这样饼才是圆的
plt.axis('equal')
plt.legend(loc='upper left')
plt.show()
其他连接:史上最全最详细python绘图手册(看我就够)——Matplotlib绘图详解
关注公众号「水沐教育科技」,在手机上阅读所有教程,随时随地都能学习。内含一款搜索神器,免费下载全网书籍和视频。

微信扫码关注公众号