前言:在anacondas环境下,导入matplotlib工具包,用matplotlib画出简单的折线图和散点图
First:折线图源码
from matplotlib import pyplot as plt
from matplotlib import font_manager
# 设置中文的方式,引入windows系统里面的字体,默认不支持中文
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/AdobeHeitiStd-Regular.otf")
# range函数传递的参数 start end step 真正执行结束的值是end的前一个值
x = range(2, 26, 2)
y = [17, 12, 10, 19, 18, 17, 14, 6, 20, 10, 11, 12]
# 指定画出来的图的大小
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
# 设置x的刻度,第三个参数不传,就表示不指定步长,间隔为1,指定y轴刻度
plt.xticks(range(2, 25))
plt.yticks(range(min(y), max(y)+1))
# 添加描述信息,添加x和y轴描述信息
plt.xlabel("X轴", fontproperties=my_font)
plt.ylabel("y轴", fontproperties=my_font)
plt.title("test-matplotlib表", fontproperties=my_font)
# 保存图片位置
plt.savefig("./01_test_折线图.png")
plt.show()
Second:散点图源码
# coding=utf-8
from matplotlib import pyplot as plt
from matplotlib import font_manager
# 引入系统字体,解决显示中文的问题
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/AdobeHeitiStd-Regular.otf")
y_3 = [14, 15, 16, 17, 16, 18, 13, 14, 15, 16, 14, 16, 17, 18, 18,
19, 15, 17, 19, 10, 14, 15, 16, 13, 14, 17, 18, 19, 10, 20, 22]
y_10 = [14, 15, 16, 17, 16, 18, 13, 14, 15, 16, 14, 16, 17, 18, 18,
19, 15, 17, 19, 10, 14, 15, 16, 13, 14, 17, 18, 19, 10, 20, 10]
# 指定画出来的图的大小
plt.figure(figsize=(20, 8), dpi=80)
x_1 = range(1, 32)
x_2 = range(51, 82)
plt.scatter(x_1, y_3, label="3月份")
plt.scatter(x_2, y_10, label="10月份")
# 调整x轴的刻度,旋转45度
_x = list(x_1)+list(x_2)
_x_labels = ["3月{}日".format(i) for i in x_1]
_x_labels += ["10月{}日".format(i-50) for i in x_2]
plt.xticks(_x[::3], _x_labels[::3], fontproperties=my_font, rotation=45)
# 添加描述信息
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度", fontproperties=my_font)
plt.title("3月份和10月份温度表", fontproperties=my_font)
# 添加图例
plt.legend(loc="upper left", prop=my_font)
plt.savefig("./01_test_散点图.png")
plt.show()