python - Pandas :在散点图中使用颜色

标签 python 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     |
--------------------------------------      

我可以为 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')

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

谢谢!

最佳答案

你可以这样做:

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

结果:

enter image description here

关于python - Pandas :在散点图中使用颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39561364/

相关文章:

python - 仅当第一行是其他下一行的子字符串时,如何对 DataFrame 行与另一行进行平均

python - 如何根据另一个 DataFrame 中的数据删除 DataFrame 的行?

python - 将不同长度的 numpy 数组保存到同一个 csv 文件的最佳方法是什么?

python - 如何计算给定 csv/psv 混合文本文件中的不同字段?

python - 在数据框的列上应用 map 功能

python - Pandas :将多列分组在一个标题下

python - 将异常传递给下一个 except 语句

python - Bokeh map 情节。纬度/经度到 x 和 y

python - 如何创建一个日志文件来记录 python 中的每个步骤?

python - 使用 .pth 文件跨 Windows 和 Linux 导入 Python 包