如何绘制具有不同颜色的相同Pandas系列色谱柱的不同部分,具有自定义索引?

时间:2021-06-24 12:02:37

This is a follow-up for my previous question here.

这是我上一个问题的后续跟进。

Let's say I have a Series like this:

假设我有一个这样的系列:

testdf = pd.Series([3, 4, 2, 5, 1, 6, 10], index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])

When plotting, this is the result:

在绘图时,这是结果:

testdf.plot()

如何绘制具有不同颜色的相同Pandas系列色谱柱的不同部分,具有自定义索引?

However, I want to plot, say, the line up to the first 4 values in blue (default) and the rest of the line in red. Trying a solution the way was suggested on the mentioned post above, this is the result I get:

但是,我想用蓝色(默认)绘制前4行的线,红线的其余部分。在上面提到的帖子中建议尝试解决方案,这是我得到的结果:

fig, ax = plt.subplots(1, 1)
testdf.plot(ax=ax,color='b')
testdf.iloc[3:].plot(ax=ax,color='r')

如何绘制具有不同颜色的相同Pandas系列色谱柱的不同部分,具有自定义索引?

I only get the expected result if I don't define my Series with a custom index:

如果我没有使用自定义索引定义我的系列,我只能得到预期的结果:

testdf = pd.Series([3, 4, 2, 5, 1, 6, 10])
fig, ax = plt.subplots(1, 1)
testdf.plot(ax=ax,color='b')
testdf.iloc[3:].plot(ax=ax,color='r')

如何绘制具有不同颜色的相同Pandas系列色谱柱的不同部分,具有自定义索引?

How could I achieve the desired result, then?

那我怎么能达到预期的效果呢?

1 个解决方案

#1


1  

I wanted to write a comment but it was too long so I write here.

我想写评论但是太长了所以我写在这里。

What you want to achieve works well in case you want to plot bars (which are discrete)

如果你想绘制条形图(它是离散的),你想要实现的效果很好

import pandas as pd
import numpy as np
df = pd.Series([3, 4, 2, 5, 1, 6, 10], index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])
df.plot(kind = 'bar',color=np.where(df.index<'e','b','r'))

But not in case of lines (which are continuous) as you already noticed.

但不是你已经注意到的线条(连续)。

In case you don't want to set custom indices you can use:

如果您不想设置自定义索引,可以使用:

 df = pd.Series([3, 4, 2, 5, 1, 6, 10])
 cut = 4
 ax = df[:cut].plot(color='b')
 df[(cut-1):].plot(ax=ax, color='r')

While using custom indices you should split your series in two parts. One example is doing

使用自定义索引时,您应该将系列分为两部分。一个例子是做

df = pd.Series([3, 4, 2, 5, 1, 6, 10], index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])
df1 = pd.Series(np.where(df.index<'e',df.values,np.nan), index=df.index)
df2 = pd.Series(np.where(df.index>='d',df.values,np.nan), index=df.index)
ax = df1.plot(color = 'b')
df2.plot(ax=ax,color='r')

#1


1  

I wanted to write a comment but it was too long so I write here.

我想写评论但是太长了所以我写在这里。

What you want to achieve works well in case you want to plot bars (which are discrete)

如果你想绘制条形图(它是离散的),你想要实现的效果很好

import pandas as pd
import numpy as np
df = pd.Series([3, 4, 2, 5, 1, 6, 10], index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])
df.plot(kind = 'bar',color=np.where(df.index<'e','b','r'))

But not in case of lines (which are continuous) as you already noticed.

但不是你已经注意到的线条(连续)。

In case you don't want to set custom indices you can use:

如果您不想设置自定义索引,可以使用:

 df = pd.Series([3, 4, 2, 5, 1, 6, 10])
 cut = 4
 ax = df[:cut].plot(color='b')
 df[(cut-1):].plot(ax=ax, color='r')

While using custom indices you should split your series in two parts. One example is doing

使用自定义索引时,您应该将系列分为两部分。一个例子是做

df = pd.Series([3, 4, 2, 5, 1, 6, 10], index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])
df1 = pd.Series(np.where(df.index<'e',df.values,np.nan), index=df.index)
df2 = pd.Series(np.where(df.index>='d',df.values,np.nan), index=df.index)
ax = df1.plot(color = 'b')
df2.plot(ax=ax,color='r')