matplotlib画散点图
程序员文章站
2024-01-19 12:57:52
...
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
font = {'family': 'MicroSoft YaHei',
'weight': 'bold',
'size': '8'}
matplotlib.rc("font", **font)
# 设置label的字体和大小
# :自动转换成 =
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False
height = [161, 170, 182, 175, 173, 165]
weight = [50, 58, 80, 70, 69, 55]
fig = plt.figure(figsize=(20, 8), dpi=80)
# 画布高20宽8,每英寸上的点数为80
plt.scatter(height, weight, s=100, c='r', marker='o', alpha=0.5)
# 散点图
# marker的类型,alpha可以更直观的显示重叠
# https://matplotlib.org/api/markers_api.html?highlight=markers#module-matplotlib.markers
xticks_label = np.arange(150, 200, 10)
plt.xticks(xticks_label, ['{}厘米'.format(i) for i in xticks_label], rotation=60)
# rotation以字符串为中心逆时针旋转90度,大于180为顺时针
min_W = np.min(weight, axis=0)
max_W = np.max(weight, axis=0)
y_step = (max_W - min_W) // len(height)
yticks_label = np.arange(min_W, max_W + y_step, y_step)
# 设置网格
plt.grid(alpha=0.7, linestyle='--', linewidth=1)
plt.xlabel("身高")
plt.ylabel("体重")
plt.title("卓信男生信息")
plt.yticks(yticks_label, [str(i) + "$kg$" for i in yticks_label])
# 设置X轴 plt.xticks([x轴的刻度], [对应刻度替换成自己想要的])
# np.arange(1,10,0.2), [random.randint(20,25) for i in range(5)]
# list[::步长]
plt.savefig('../Images/h_w.png')
# 保存成svg矢量图格式,上一个目录下的Images文件夹下面
plt.show()