導(dǎo)入數(shù)據(jù)
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data=pd.read_csv("graph.csv")
data
兩個數(shù)值變量
plt.xlabel('height')
plt.ylabel('weight')
plt.scatter(data['height'],data['weight'])
plt.grid() # 生成網(wǎng)格
plt.show()

1.png
三個數(shù)值變量
plt.xlabel('height')
plt.ylabel('weight')
m = {"M":'o',"F":'s'}
cm = list(map(lambda x:m[x],data.sex))#將相應(yīng)的標簽改為對應(yīng)的marker
print(cm)
plt.scatter(data.height,data.weight,s=data.income*4)
plt.grid() # 生成網(wǎng)格
plt.show()

image.png
四個數(shù)值變量
plt.xlabel('height')
plt.ylabel('weight')
plt.scatter(data.height,data.weight,s=data.income*4,c=data.score, cmap='spring',alpha=0.3)
plt.colorbar()
plt.grid() # 生成網(wǎng)格
plt.show()

image.png
四個數(shù)值變量與一個分類變量
plt.xlabel('height')
plt.ylabel('weight')
mdata=data.loc[data['sex']=='M']
fdata=data.loc[data['sex']=='F']
plt.scatter(mdata.height, mdata.weight,s= mdata.income*4,c=mdata.score,marker="o", cmap='spring',alpha=0.3)
plt.scatter(fdata.height, fdata.weight,s= fdata.income*4,c=fdata.score,marker="s", cmap='spring',alpha=0.3)
plt.colorbar()
plt.grid() # 生成網(wǎng)格
plt.show()

image.png
五個數(shù)值變量與一個分類變量
plt.xlabel('height')
plt.ylabel('weight')
mdata=data.loc[data['sex']=='M']
fdata=data.loc[data['sex']=='F']
plt.scatter(mdata.height, mdata.weight,s= mdata.income*4,linewidths=mdata.cost*0.2,c=mdata.score,marker="o", cmap='spring',alpha=0.3)
plt.scatter(fdata.height, fdata.weight,s= fdata.income*4,linewidths=fdata.cost*0.2,c=fdata.score,marker="s", cmap='spring',alpha=0.3)
plt.colorbar()
plt.grid() # 生成網(wǎng)格
plt.show()

image.png
結(jié)論
- 可視化不應(yīng)使數(shù)據(jù)失真。
- 可視化不應(yīng)包含不必要的裝飾。
- 所有軸均應(yīng)正確標記。
- 考慮合適的圖形與配色。
- 變量的數(shù)量不應(yīng)太多。