【问题标题】:Color coding or labelling the scatter plot of a pandas dataframe?对熊猫数据框的散点图进行颜色编码或标记?
【发布时间】:2016-10-24 23:59:26
【问题描述】:

我有一个要在 pandas 中绘制的数据框:

import pandas as pd
df = pd.read_csv('Test.csv')
df.plot.scatter(x='x',y='y')

数据框有 3 列

    x   y result
 0  2   5  Good 
 1  3   2    Bad
 2  4   1    Bad
 3  1   1  Good 
 4  2  23    Bad
 5  1  34  Good

我想格式化散点图,使得如果 df['result']='Good' 每个点为绿色,如果 df['result']='Bad' 则为红色。

可以使用 pd.plot 做到这一点,还是有办法使用 pyplot 做到这一点?

【问题讨论】:

标签: python pandas matplotlib plot dataframe


【解决方案1】:
df.plot.scatter('x', 'y', c=df.result.map(dict(Good='green', Bad='red')))

【讨论】:

    【解决方案2】:

    一种方法是在同一轴上绘制两次。首先我们只绘制“好”点,然后我们只绘制“坏”点。诀窍是在scatter 方法中使用ax 关键字,如下所示:

    ax = df[df.result == 'Good'].plot.scatter('x', 'y', color='green')
    df[df.result == 'Bad'].plot.scatter('x', 'y', ax=ax, color='red')
    

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 2017-05-07
      • 2015-03-29
      • 2017-05-19
      • 2019-01-03
      • 2018-12-28
      • 1970-01-01
      • 2017-04-20
      • 2021-12-28
      相关资源
      最近更新 更多