例子1
from matplotlib import pyplot as plt
x = range(2, 26, 2)
y = [14, 12, 34, 12, 34, 12, 34, 12, 33, 43, 32, 11]
# figsize: 图片大小
# dpi:图片dpi,可以提高清晰度
fig = plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
# 设置x轴刻度
plt.xticks(range(2, 25))
plt.yticks(range(min(y), max(y) + 1))
plt.savefig('./sig_size.png')
# svg放大不会失真
plt.savefig('./sig_size.svg')
plt.show()
例子2
import random
from matplotlib import pyplot as plt
if __name__ == '__main__':
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]
plt.figure(dpi=80)
plt.plot(x, y)
# 调整x轴的刻度
_x = list(x)[::20]
_x_ticks_labels = ["hello,{}".format(i) for i in _x]
# rotation旋转的角度
plt.xticks(_x, _x_ticks_labels, rotation=45)
plt.xlabel("Time")
plt.ylabel("Num")
plt.title("Num Count")
plt.show()
例子3
from matplotlib import pyplot as plt
# matplotlib的官网:https://matplotlib.org/gallery/index.html
if __name__ == '__main__':
y1 = [1, 0, 2, 3, 5, 6, 7, 9, 10, 34]
y2 = [2, 3, 4, 0, 4, 7, 8, 9, 23, 66]
x = range(10, 20)
# 指定颜色,指定标签
# 还有很多属性,具体可以百度
plt.plot(x, y1, label='yours', color='orange', linewidth=3)
plt.plot(x, y2, label='ours', color='gray', linewidth=3)
plt.xticks(x, ["{} years".format(i) for i in x], rotation=45)
plt.xlabel("years")
plt.ylabel("nums")
plt.title("years and nums")
# 绘制网格
plt.grid(alpha=0.5)
# 添加图例
# loc 设置图例位置
plt.legend(loc="upper right")
plt.show()