最基础、最简陋的matplotlib入门学习
目的是为了记录所使用的方法
导入包用 import matplotlib.pyplot as plt
要学好还是要看官方文档
例子传送门 教程传送门
1.基本配置
- pip install matplotlib
- 在matplotlib中添加中文字体,我之前参考的博客找不到了,反正就是永久性修改,省事
2.绘制简易图形的相关方法
- 折线图的描绘:plot(*args, scalex=True, scaley=True, data=None, **kwargs)
常用的是 plt.plot(x, y, label=“线条标签”, color=“颜色”, linestyle="–", linewidth=5, alpha=0.6)
其中# linestyle=线条格式, linewidth=线粗, alpha=透明度,
在kwargs字典序里面还有很多好用的,建议通过源代码查询的方式了解更多 - 散点图的描绘:scatter()
直接参考这一篇讲scatter的博客就好了,讲的蛮清楚的 - 条形图的描绘:有两种方法,一个画竖形图,一个画横行图:
plt.bar(x, y, width=0.5, color=‘Blue’, label=‘竖形图’)
plt.barh(x, y, height=0.5, color=‘Red’, label=‘横形图’) - 直方图的描绘:plt.hist(a, num_bins, density=True)
其中a为数组,num_binis为间距,density为y轴百分比化
直接参考这一篇讲hist的博客
3.完善所绘图形
-
设置图形大小
plt.figure(figsize=(16, 8), dpi=80) -
设置轴的刻度
plt.xticks(list(x), xtick, rotation=45) 其中rotation 为旋转角度
plt.yticks(range(min(y), max(y)+1))
或者:
plt.tick_params(axis=‘both’, labelsize=14) 此方法具体看源代码或者参考此博客 -
添加描述信息
plt.xlabel(“x轴信息”)
plt.ylabel(“y轴信息”)
plt.title(“标题信息”)
查看源代码进行改字体等操作 -
添加图例:如将图例放在左上角
plt.legend(loc=“upper left”) -
绘制网格
plt.grid(axis =“both"alpha=0.8, linestyle=”–", linewidth=5)
alpha 透明度
linestyle 线条格式
linewidth 线条宽度
可以参考这篇博客 -
保存所绘之图
plt.savefig("./{}.png".format(“图片名称”)) -
展示图片
plt.show()
4.相关实例
- 折线图
# coding=utf-8
# 折线图的描绘(plt.plot)
import matplotlib.pyplot as plt
import random
x = range(0, 12)
y = [random.randint(20, 35) for i in range(12)]
y_ = [random.randint(20, 35) for i in range(12)]
# 设置图片大小
plt.figure(figsize=(12, 6), dpi=80)
# 绘图(折线)
plt.plot(x, y, label="今天", color="orange", linestyle="--", linewidth=2, alpha=0.6)
plt.plot(x, y_, label="昨天", color="cyan", linestyle="-.", linewidth=2)
# 设置轴的刻度
xtick_labels = ["第{}小时".format(i+1) for i in range(12)]
plt.xticks(list(x), xtick_labels, rotation=45) # rotation 为旋转角度
plt.yticks(range(min(y), max(y)+1))
# 添加描述信息
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("12小时每小时气温变化")
# 绘制网格
plt.grid(alpha=0.8, linestyle="--") # alpha 透明度
# 展示图片
plt.show()
显示结果:
2. 散点图
# coding=utf-8
# 散点图的描绘(plt.scatter)
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)
x = np.array(range(9), dtype=int)
y_1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
y_2 = np.array([i**2 for i in y_1])
colors = np.random.rand(9)
area_1 = y_1 ** 2
area_2 = y_2 ** 2
# 绘制散点图
plt.scatter(x, y_1, s=area_1, c=colors, alpha=0.9)
plt.scatter(x, y_2, s=area_2, c=colors, alpha=0.8)
plt.show()
3. 条形图的绘制
# coding=utf-8
# 散点图的描绘(plt.bar)
import matplotlib.pyplot as plt
x = ["点赞", "投币", "收藏", "转发", "评论"]
y_3 = [12, 25, 15, 10, 15]
y_2 = [14, 35, 35, 15, 20]
y_1 = [16, 45, 55, 25, 25]
bar_wight = 0.2
y_11 = range(len(y_1))
y_12 = [i+bar_wight for i in y_11]
y_13 = [i+bar_wight*2 for i in y_11]
plt.figure(figsize=(16, 8), dpi=80)
# 绘制条形图
plt.barh(range(len(y_11)), y_1, height=bar_wight, label="1月1") # 横形图
plt.barh(y_12, y_2, height=bar_wight, label="1月2") # 横形图
plt.barh(y_13, y_3, height=bar_wight, label="1月3") # 横形图
# 添加描述信息
plt.xlabel("数量")
plt.ylabel("类型")
plt.title("the 变化")
plt.yticks(y_12, x)
plt.legend()
plt.show()
4. 直方图的绘制
# coding=utf-8
# 直方图的的描绘(plt.hist)
import matplotlib.pyplot as plt
a = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 11, 12, 13, 11]
# 计算组距
d = 1
num_bins = (max(a) - min(a))//d # 组数
plt.hist(a, num_bins)
plt.grid()
plt.xticks(range(min(a), max(a)+d, d))
plt.yticks(range(5))
plt.show()