根据 pandas 中的列值上色时间序列 [英] Color time-series based on column values in pandas

查看:77
本文介绍了根据 pandas 中的列值上色时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pandas DataFrame中有一个时间序列(在示例中为df.data),并希望基于另一列的值(在示例中为df.colors;为0、1和2)为绘图着色在这种情况下,但如果它也可以与float s一起使用,那将是一件好事/可移植性.

I have a time-series in a pandas DataFrame (df.data in the example) and want to color the plot based on the values of another column (df.colorsin the example; values are 0, 1, and 2 in this case, but it would be good / more portable if it would also work with floats).

import pandas as pd
n = 10
seed(1)
df = pd.DataFrame(data={"data":randn(n), "colors":randint(0,3,n)},
                  index=pd.date_range(start="2016-01-01", periods=n))

df.data.plot(style=".", ms=10)

我正在寻找的是类似

df.data.plot(style=".", color=df.colors)

(不起作用),以生成如下图:

(which does not work), in order to produce a plot like this:

在这里,标记分别用红色,橙色和绿色分别表示colors==012.手动进行少量数据和少量颜色操作相对容易,但是有没有一种直接自动进行此操作的简单方法呢?

Here the markers are colored red, orange, and green, for colors==0, 1, and 2, respectively. It's relatively easy to do this manually for few data and few colors, but is there a straightforward way to do this automatically?

似乎有一种使用plt.scatter和颜色图的解决方案,如

There seems to be a solution using plt.scatter and colormaps, as shown in the answer to How to use colormaps to color plots of Pandas DataFrames, but using plt.scatter with a datetime index destroys the convenient automatic axis scaling of using df.data.plot(...). Is there a way using this notation?

推荐答案

实现此目标的一种方法是使用

One way to achieve this would be to use DF.replace and create a nested dictionary to specify the color values for the int/float values to be mapped against.

plt.style.use('seaborn-white')
df.replace({'colors':{0:'red',1:'orange',2:'green'}}, inplace=True)

然后您可以执行 DF.groupby ,以便在每个迭代步骤中groupby对象的每个子组的颜色保持相同.

You could then perform DF.groupby on it to keep the colors same for each subgroup of the groupby object on every iteration step.

for index, group in df.groupby('colors'):
    group['data'].plot(style=".", x_compat=True, ms=10, color=index, grid=True)

这篇关于根据 pandas 中的列值上色时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆