我最喜欢ggplot2
在 R 中使用库的一个方面是能够轻松指定美学。我可以快速制作一个散点图并应用与特定列相关的颜色,我希望能够使用 python/pandas/matplotlib 来做到这一点。我想知道人们是否有任何便利功能可以使用熊猫数据框和 Matplotlib 将颜色映射到值?
##ggplot scatterplot example with R dataframe, `df`, colored by col3
ggplot(data = df, aes(x=col1, y=col2, color=col3)) + geom_point()
##ideal situation with pandas dataframe, 'df', where colors are chosen by col3
df.plot(x=col1,y=col2,color=col3)
编辑:感谢您的回复,但我想包含一个示例数据框来澄清我的要求。两列包含数字数据,第三列是分类变量。我正在考虑的脚本将根据此值分配颜色。
np.random.seed(250)
df = pd.DataFrame({'Height': np.append(np.random.normal(6, 0.25, size=5), np.random.normal(5.4, 0.25, size=5)),
'Weight': np.append(np.random.normal(180, 20, size=5), np.random.normal(140, 20, size=5)),
'Gender': ["Male","Male","Male","Male","Male",
"Female","Female","Female","Female","Female"]})
Height Weight Gender
0 5.824970 159.210508 Male
1 5.780403 180.294943 Male
2 6.318295 199.142201 Male
3 5.617211 157.813278 Male
4 6.340892 191.849944 Male
5 5.625131 139.588467 Female
6 4.950479 146.711220 Female
7 5.617245 121.571890 Female
8 5.556821 141.536028 Female
9 5.714171 134.396203 Female