matplotlib 散点图

一、特点

离散的数据,查看分布规律,走向趋势

二、使用

1、核心

plt.scatter(x, y)
# x为x轴的数据,可迭代对象,必须是数字
# y为y轴的数据,可迭代对象,必须是数字
# x和y必须一一对应

2、例子

注意:在设置x轴或y轴刻度时,ticks和labes的值要一一对应

from matplotlib import pyplot as plt
from matplotlib import font_manager


a = [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]
b = [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]

# 设置中文
my_font = font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc')

x_a = range(1, 32)
x_b = range(51, 82)
# 设置图形大小
plt.figure(figsize=(20, 8), dpi=80)

# 绘图
plt.scatter(x_a, a, label="3月份")
plt.scatter(x_b, b, label="4月份")

# 定制x轴的刻度和label
_x = list(x_a) + list(x_b)
x_labels = ["3月{}日".format(i) for i in x_a]
x_labels += ["11月{}日".format(i) for i in x_a]
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月份和11月份的温度", fontproperties=my_font)

# 添加图例
plt.legend(prop=my_font)
# 展示图片
plt.show()

猜你喜欢

转载自www.cnblogs.com/wt7018/p/11946178.html