matplotlib:散点图
和折线图类似,通用部分可查看折线图
适用场景:
看整个数据离散和聚合程度
两变量的相关性
import matplotlib.pyplot as plt
from matplotlib import rcParams
import random# Windows自带的黑体
rcParams['font.family'] = 'SimHei'
# 创建图表,设置大小
plt.figure(figsize = (10,8))x = []
y = []
for i in range(1000):tmp = random.uniform(0,10)x.append(tmp)tmp2 = 2*tmp + random.gauss(0,2)y.append(tmp2)plt.scatter(x,y,color = 'blue',alpha = 0.5,s = 20,label = "数据")plt.title("X变量与Y变量的关系",color = 'red',fontsize = 20)plt.xlabel("X自变量",fontsize = 10)
plt.ylabel("Y因变量",fontsize = 10)plt.legend(loc = "upper left")plt.grid(True,alpha = 0.1,color = 'blue',linestyle = '--')plt.xticks(rotation = 0,fontsize = 12)
plt.yticks(rotation = 0,fontsize = 12)plt.ylim(0,30)# 显示图表
plt.show()
添加回归线
plt.plot([0,10],[0,20],color = 'red',linewidth = 2,linestyle = '-')