【问题标题】:How to specify colors for individual points in a scatter plot using Pandas如何使用 Pandas 为散点图中的各个点指定颜色
【发布时间】:2015-03-27 08:01:11
【问题描述】:

我的问题如下。

我有一个 pandas DataFrame,其中包含第一行中的“样本”数据和所有其他行中的“控件”数据。 我想要一个散点图(或任何其他类型的图来概括问题),其中所有“控件”都是一种颜色,而“样本”是另一种颜色。怎么做?我查看了 pandas 文档,但找不到任何东西。

这是我目前所拥有的

from pandas import *
from collections import OrderedDict

mydict = OrderedDict([
                    ('sample', [454, 481, 160, 26, 17]),
                    ('ctrl_1', [454, 470, 101, 10, 8]),
                    ('ctrl_2', [454, 473, 110, 15, 9]),
                    ('ctrl_3', [454, 472, 104, 19, 13]),
                    ('ctrl_4', [454, 472, 105, 16, 13]),
                    ('ctrl_5', [454, 466, 97, 15, 10]),
                    ('ctrl_6', [454, 473, 110, 17, 10]),
                    ('ctrl_7', [454, 465, 99, 15, 11]),
                    ('ctrl_8', [454, 471, 107, 18, 12]),
                    ('ctrl_9', [454, 471, 102, 15, 11]),
                    ('ctrl_10', [454, 472, 116, 14, 9])
                    ])

df = DataFrame.from_dict(mydict,orient='index')
df.columns=['A','B','C','D','E']

df.plot(kind='scatter',x='C',y='E',figsize=(10,10), color='blue')

我尝试将 DataFrame 拆分为两部分(控件和示例)并将一个绘制在另一个之上,但是当您尝试对单个点进行散点图(这是一个错误吗?)。

sample = df.ix[0]
controls = df.ix[1:]
controls.plot(kind='scatter',x='C',y='E',figsize=(10,10), color='blue')
sample.plot(kind='scatter',x='C',y='E',figsize=(10,10), color='red')

有什么建议吗?

【问题讨论】:

    标签: python pandas colors scatter-plot


    【解决方案1】:

    您将收到来自df.ix[0] 的系列回复,其中can't be drawn as a scatter plot。 (我想理论上它可能是一个有效的类型,但是,正如你所说,它只会显示 1 分。)

    如果您稍微更改代码以将 sample 改为 DataFrame,则它可以工作。 (我还使用相同的轴将两者放在同一个图上。)

    sample = df.ix[:1]
    controls = df.ix[1:]
    ax = controls.plot(kind='scatter',x='C',y='E',figsize=(10,10), color='blue')
    sample.plot(ax=ax, kind='scatter',x='C',y='E',figsize=(10,10), color='red')
    

    【讨论】:

    • 完美运行!谢谢你。不知道 df.ix[0] 和 df.ix[:1] 不一样!
    • 太棒了!我被“它是 DataFrame 还是 Series?”所吸引。过去多次提出问题... :)
    猜你喜欢
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多