pandas 熊猫:在散点图中使用颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39561364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:02:02  来源:igfitidea点击:

pandas: Using color in a scatter plot

pythonpandasdataframe

提问by Edamame

I have a pandas dataframe:

我有一个Pandas数据框:

--------------------------------------
|   field_0  |  field_1   | field_2  |
--------------------------------------
|     0      |   1.5      |  2.9     |
--------------------------------------
|     1      |   1.3      |  2.6     |
--------------------------------------
|       :                            |
--------------------------------------
|     1      |   2.1      |  3.2     |
--------------------------------------
|     0      |   1.1      |  2.1     |
--------------------------------------      

I can create a scatter plot for field_1 vs. field_2 like below:

我可以为 field_1 与 field_2 创建一个散点图,如下所示:

%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
my_df.plot(x='field_1', y='field_2', kind = 'scatter')

However, I am wondering is it possible to include field_0 in the plot? So when field_0 = 0, the point is blue; when field_1 = 1, the point is red.

但是,我想知道是否可以在图中包含 field_0?所以当field_0 = 0时,点是蓝色的;当 field_1 = 1 时,该点为红色。

Thanks!

谢谢!

回答by MaxU

you can do it this way:

你可以这样做:

col = df.field_0.map({0:'b', 1:'r'})
df.plot.scatter(x='field_1', y='field_2', c=col)

Result:

结果:

enter image description here

在此处输入图片说明