散点图,顾名思义是一些散乱的点构成的图。那么这些散乱的点有什么作用呢?散点图通过用两组数据构成多个坐标点,考察坐标点的分布,判断两变量之间是否存在某种关联或总结坐标点的分布模式。
绘制方法大体上与折线图一致,只是对点不需要去拟合折线,使用plt.scatter()函数替代plt.plot()即可。例如绘制三月份与十一月份的气温散点图,代码如下:
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
|
''' 绘制散点图,要点:plt.scatter(x,y) ''' # 导入模块 from matplotlib import pyplot as plt from matplotlib import font_manager my_font = font_manager.FontProperties(fname = "C:\Windows\Fonts\MSYHL.TTC" ) # 输入变量数据(参数) y_3 = [ 11 , 17 , 16 , 11 , 12 , 11 , 12 , 6 , 6 , 7 , 8 , 9 , 12 , 15 , 14 , 17 , 18 , 21 , 16 , 17 , 20 , 14 , 15 , 15 , 15 , 19 , 21 , 22 , 22 , 22 , 23 ] y_10 = [ 26 , 26 , 28 , 19 , 21 , 17 , 16 , 19 , 18 , 20 , 20 , 19 , 22 , 23 , 17 , 20 , 21 , 20 , 22 , 15 , 11 , 15 , 5 , 13 , 17 , 10 , 11 , 13 , 12 , 13 , 6 ] x_3 = range ( 1 , 32 ) x_10 = range ( 51 , 82 ) # 设置图形大小 plt.figure(figsize = ( 20 , 8 ),dpi = 80 ) # 使用scatter绘制散点图,和之前绘制折线图一样只用将plot更改成scatter plt.scatter(x_3,y_3,label = '3月份气温变化散点图' ) plt.scatter(x_10,y_10,label = '10月份气温变化散点图' ) # 调整X轴的刻度 _x = list (x_3) + list (x_10) _xtick_labels = [ '3月{}日' . format (i) for i in x_3] _xtick_labels + = [ '10月月{}日' . format (i - 50 ) for i in x_10] plt.xticks(_x[:: 3 ],_xtick_labels[:: 3 ],fontproperties = my_font,rotation = 45 ) # 添加描述信息 plt.xlabel( '时间' ,fontproperties = my_font) plt.ylabel( '温度' ,fontproperties = my_font) plt.title( '三月气温和十月气温散点图' ,fontproperties = my_font) # 添加图例 plt.legend(prop = my_font,loc = 'upper left' ) # 要在绘制图像那一步添加标签 # 展示图形 plt.show() |
运行结果如下:
画图方式与折线图大体一致,只是修改了拟合的方式。从图中能大体看出三月份气温呈现上升趋势,十一月份呈现下降趋势,且增长与下降幅度变化大体一致。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_52131774/article/details/121443859