matplotlib进行绘图——散点图
分为七个步骤:
1、导入模块
2、设置绘图风格
3、导入数据
4、设置图框的大小
5、绘图
6、添加轴标签和标题
7、显示图形
# 导入第三方模块
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import pandas as pd
# 设置设置绘图风格
print (plt.style.available)
plt.style.use("Solarize_Light2")
plt.rcParams['font.sans-serif']= 'SimHei'
# 导入数据
df = pd.read_csv(r"C:\Users\guanyang\Desktop\python_scatter\cars.csv")
# 设置图框的大小
fig = plt.figure(num =3, figsize=(10, 6), facecolor='y')
# 绘图
plt.scatter(df.speed, # x轴数据为汽车速度
df.dist, # y轴数据为汽车的刹车距离
s = 30, # 设置点的大小
c = 'b', # 设置点的颜色
marker = 'o', # 设置点的形状
alpha = 0.9, # 设置点的透明度
linewidths = 0.3, # 设置散点边界的粗细
label = '观测点'
)
# 添加轴标签和标题
plt.title('汽车速度与刹车距离的关系')
plt.xlabel('汽车速度')
plt.ylabel('刹车距离')
# 显示图形
fig.autofmt_xdate(rotation = 45)
plt.legend(title = "图例", loc = 'best')
plt.show()
bingo: