數(shù)據(jù)可視化:散點圖的成長史(Python)

導(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)太多。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容