Pandas 数据帧的条件matplotlib fill_

Pandas 数据帧的条件matplotlib fill_,pandas,matplotlib,Pandas,Matplotlib,这似乎很容易,但我很挣扎。 我希望有条件地在轴之间着色,即使区域填充为绿色或红色并完全填充。我使用一些布尔df列来确定颜色 df = pd.DataFrame([1,1,1,1,1,0,0,1,1,0,1,1,0,1,0,1], columns=['grow']) df['green']=(df.grow>0) | (df.grow.shift(1)>0) df['red']= (df.grow<=0) | (df.grow.shift(1)<=0) df=pd.Da

这似乎很容易,但我很挣扎。 我希望有条件地在轴之间着色,即使区域填充为绿色或红色并完全填充。我使用一些布尔df列来确定颜色

df = pd.DataFrame([1,1,1,1,1,0,0,1,1,0,1,1,0,1,0,1], columns=['grow'])
df['green']=(df.grow>0) | (df.grow.shift(1)>0)
df['red']= (df.grow<=0) | (df.grow.shift(1)<=0) 
df=pd.DataFrame([1,1,1,1,0,0,1,1,1,0,0,1,1,0,1,0,0,1],列=['grow']))
df['green']=(df.grow>0)|(df.grow.shift(1)>0)

df['red']=(df.grow当您将
pd.grow
本身绘制为线图时,填充区域不连续的原因变得很明显。使用
fill_between()
的方式,您隐含地假设您的数据类似于阶跃函数,但实际上它更像锯齿(边缘不是“尖锐的”)。解决此问题的一种方法是使用更多重复的值填充函数,从而使0和1之间的转换更清晰。
numpy
是此类操作的实用工具。下面是一个如何执行此操作的示例:

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame([1,1,1,1,1,0,0,1,1,0,1,1,0,1,0,1], columns=['grow'])
df['green']=(df.grow>0) | (df.grow.shift(1)>0)
df['red']= (df.grow<=0) | (df.grow.shift(1)<=0)

fig, axes = plt.subplots(
    nrows=2, ncols=2, gridspec_kw = {'height_ratios':[1, 3]}
)

axes[0,0].plot(df.index, df.grow)
axes[0,0].set_title('original function')

axes[1,0].fill_between(df.index, 0, 1,  
                 where=(df.grow>0) , color = 'green', alpha = 0.1)
axes[1,0].fill_between(df.index, 0, 1,  
                 where=(df.grow<=0) , color = 'red', alpha = 0.1)
axes[1,0].set_title('original shading')

N=100
x = np.linspace(df.index[0],df.index[-1],N*len(df.index))
y = np.repeat(df.grow, N)

axes[0,1].plot(x,y)
axes[0,1].set_title('sharper step function')

axes[1,1].fill_between(x, 0, 1,  
                 where=(y>0) , color = 'green', alpha = 0.1, lw=0)
axes[1,1].fill_between(x, 0, 1,  
                 where=(y<=0) , color = 'red', alpha = 0.1,lw=0)
axes[1,1].set_title('new_shading')


plt.show()
从matplotlib导入pyplot作为plt
作为pd进口熊猫
将numpy作为np导入
df=pd.DataFrame([1,1,1,1,0,0,1,1,1,0,1,1,0,1,0,1],columns=['grow']))
df['green']=(df.grow>0)|(df.grow.shift(1)>0)

df['red']=(df.grow您的区间是如何定义的?如果一列的
grow==1
(比如
x==1
),那么绿色阴影应该介于
-0.5
0.5
之间还是介于
1
2
之间?
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame([1,1,1,1,1,0,0,1,1,0,1,1,0,1,0,1], columns=['grow'])
df['green']=(df.grow>0) | (df.grow.shift(1)>0)
df['red']= (df.grow<=0) | (df.grow.shift(1)<=0)

fig, axes = plt.subplots(
    nrows=2, ncols=2, gridspec_kw = {'height_ratios':[1, 3]}
)

axes[0,0].plot(df.index, df.grow)
axes[0,0].set_title('original function')

axes[1,0].fill_between(df.index, 0, 1,  
                 where=(df.grow>0) , color = 'green', alpha = 0.1)
axes[1,0].fill_between(df.index, 0, 1,  
                 where=(df.grow<=0) , color = 'red', alpha = 0.1)
axes[1,0].set_title('original shading')

N=100
x = np.linspace(df.index[0],df.index[-1],N*len(df.index))
y = np.repeat(df.grow, N)

axes[0,1].plot(x,y)
axes[0,1].set_title('sharper step function')

axes[1,1].fill_between(x, 0, 1,  
                 where=(y>0) , color = 'green', alpha = 0.1, lw=0)
axes[1,1].fill_between(x, 0, 1,  
                 where=(y<=0) , color = 'red', alpha = 0.1,lw=0)
axes[1,1].set_title('new_shading')


plt.show()