散点图万能模板
# 读取数据 df = pd.read_csv('iris.csv') def get_xycs(df): # 平面坐标系的位置只能表示2维数据 x = df['sepal_length'] # x 轴坐标 y = df['sepal_width'] # y 轴坐标 c = df['petal_length'] # 颜色color s = df['petal_width'] # 大小size return x,y,c,s markers = {'setosa':'o', 'versicolor':'D', 'virginica':'*'} # 根据X,Y值画散点图, 用颜色的深浅表示花萼的长度,用大小表示花萼的宽度, 每组数据只能是一种点样式 plt.figure(figsize=(5,5),dpi=100) #plt.scatter(x,y, c=c, s=50) # 可以是标量,那么所有的点都一样 for sp in df['species'].unique(): x,y,c,s = get_xycs(df[df['species']==sp]) plt.scatter(x,y, s=s*30, cmap=plt.cm.seismic, marker=markers[sp],label=sp) plt.legend()
其他模板
### 在二维坐标系上,位置表示(x,y)二维数据 x = df.sepal_length # x 表示花瓣长 y = df.sepal_width # y 表示花瓣宽 s = (df.petal_length * df.petal_width)*np.pi # s(size) 表示花萼面积 c = (df.petal_length * df.petal_width)*np.pi plt.scatter(x,y,s=s*5, c=c,cmap=plt.cm.RdYlBu_r) plt.xlabel('sepal_length') plt.ylabel('sepal_width')
# 在二维坐标系上,位置表示(x,y)二维数据 x = df.sepal_length # x 表示花瓣长 y = df.sepal_width # y 表示花瓣宽 s = (df.petal_length * df.petal_width)*np.pi # s(size) 表示花萼面积 #print(df.species) #colormap = {"setosa":"#FF0000", "versicolor":"green", "virginica":"b"} # 定义一个字典将species字符串映射到颜色字符串上 colormap = {"setosa":1, "versicolor":5, "virginica":6} # 定义一个字典将species字符串映射到颜色字符串上 c = df.species.map(colormap) #print(c) plt.scatter(x,y,s=s*5, c=c,cmap=plt.cm.coolwarm, alpha=0.7, edgecolors='face') plt.xlabel('sepal_length') plt.ylabel('sepal_width')
plt.scatter(df['burglary'], df['larceny_theft'], s=df['population']*2e-5, c=df['motor_vehicle_theft'], cmap=plt.cm.coolwarm, edgecolors='b', alpha=0.75) for idx,statename in df['state'].items(): plt.text(x=df['burglary'][idx],y=df['larceny_theft'][idx]-df['population'][idx]*2e-5*0.5,s=statename,fontsize=6,ha='center',va='top')
df.plot.scatter(x='burglary',y='larceny_theft',c='motor_vehicle_theft',cmap=plt.cm.coolwarm,s=df['population']*2e-5) for i in df.index: if i in top5_motor_theft_index: # 偷车贼最多的5个州 plt.text(df.loc[i,'burglary']+10, df.loc[i,'larceny_theft']-10, df.loc[i,'state'], color='red') # 一个文本框