cmi*_*er8 2 python time-series matplotlib pandas
根据DataFrame中的一列来遮蔽pandas子图的最优雅方法是什么?
一个简单的例子:
In [8]:
from random import *
import pandas as pd
randBinList = lambda n: [randint(0,1) for b in range(1,n+1)]
rng = pd.date_range('1/1/2011', periods=72, freq='H')
ts = pd.DataFrame({'Value1': randn(len(rng)),'Value2': randn(len(rng)),'OnOff': randBinList(len(rng))}, index=rng)
ts.plot(subplots=True)
Run Code Online (Sandbox Code Playgroud)
结果如下:

理想情况下,我想要一个正好的子图,Value1并且Value2两个图都被阴影使用axvspanwhere On(1.0在中的值OnOff)被着色并且Off没有阴影.
你的设置非常好.不过,我认为你需要直接与matplotlib进行交互.
如果你设置你的DataFrame(你已经拥有):
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
randBinList = lambda n: [np.random.randint(0,2) for b in range(1,n+1)]
rng = pd.date_range('1/1/2011', periods=72, freq='H')
ts = pd.DataFrame({
'Value1': np.random.randn(len(rng)),
'Value2': np.random.randn(len(rng)),
'OnOff': randBinList(len(rng))
}, index=rng)
Run Code Online (Sandbox Code Playgroud)
然后你可以使用fill_between命令与wherekwarg:
fig, (ax1, ax2) = plt.subplots(nrows=2)
ax1.plot(ts.index, ts['Value1'], 'k-')
ax1.fill_between(ts.index, ts['Value1'], y2=-6, where=ts['OnOff'])
ax2.plot(ts.index, ts['Value2'], 'k-')
ax2.fill_between(ts.index, ts['Value2'], y2=-6, where=ts['OnOff'])
fig.tight_layout()
Run Code Online (Sandbox Code Playgroud)
这给了我:

| 归档时间: |
|
| 查看次数: |
1714 次 |
| 最近记录: |