兩個散點圖比較

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.rc
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]

height1 = [161, 170, 182, 175, 173, 165]
weight2 = [50, 56, 69, 53, 35, 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, label='卓信')

plt.scatter(height1, weight2, s=100, c='b', marker='o', alpha=0.5, label='普信')
# 散點圖
# 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.2)
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矢量圖格式
plt.legend(loc=2)

# 添加圖例
plt.show()

結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章