Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

Gray-Ice

个人博客兼个人网站

使用matplotlib绘制散点图

使用pyplot.scatter方法绘制散点图,其他步骤和绘制折线图基本没有什么不同。

下面是代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from matplotlib import pyplot as plt
from matplotlib import font_manager


# 设置字体
my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\simkai.ttf")
y_3 = [11, 17, 16, 11, 12, 11, 12, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16]
y_10 = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17, 20, 21, 20]
# 设置图形大小
plt.figure(figsize=(20, 8), dpi=80)

x = range(1, 19)
x_10 = range(19, 37)
print(len(y_3), len(y_10))
# 绘制散点图
plt.scatter(x, y_3, label='三月份')
print(len(x_10), len(y_10))
# 绘制散点图
plt.scatter(x_10, y_10, label='十月份')

# 调整x轴的刻度
_x = list(x) + list(x_10)

# x轴刻度
_xtick_labels = ["三月{}日".format(i) for i in x]
# x轴刻度
_xtick_labels += ["十月{}日".format(i-19) for i in x_10]
# 设置x轴刻度,设置字体,设置字体旋转
plt.xticks(_x[::3], _xtick_labels[::3], fontproperties=my_font, rotation=45)
# 设置x轴标签
plt.xlabel('时间', fontproperties=my_font)
# 设置y轴标签
plt.ylabel('温度', fontproperties=my_font, rotation=1)
# 设置图例
plt.legend(loc='upper right', prop=my_font)
# 保存图片
plt.savefig('./fire.png')

效果如图所示:

image-20200716002726974

评论



愿火焰指引你