我无法让它适用于我的数据,所以首先我尝试一个非常相似的具体示例。这是数据框:
In [56]:
idx = pd.DatetimeIndex(start='1990-01-01', freq='d', periods=5)
data= pd.DataFrame({('A','a'):[1,2,3,4,5],
('A','b'):[6,7,8,9,1],
('B','a'):[2,3,4,5,6],
('B','b'):[7,8,9,1,2]}, idx)
Out[56]:
A B
a b a b
1990-01-01 1 6 2 7
1990-01-02 2 7 3 8
1990-01-03 3 8 4 9
1990-01-04 4 9 5 1
1990-01-05 5 1 6 2
所以我希望做的是绘制一个时间序列,其中每个观察(索引中的每一天)的变量(每列)之间的集中趋势线,阴影区域表示指定的误差估计量(可能只有 95 % ci) 对应于每一天的观察结果。
我试过这个:
sns.tsplot(data, time=idx)
但我收到以下错误:
UnboundLocalError Traceback (most recent call last)
<ipython-input-57-fa07e08ead95> in <module>()
5 ('B','b'):[7,8,9,1,2]}, idx)
6
----> 7 sns.tsplot(data, time=idx)
C:\Users\Patrick\Anaconda\lib\site-packages\seaborn\timeseries.pyc in tsplot(data, time, unit, condition, value, err_style, ci, interpolate, color, estimator, n_boot, err_palette, err_kws, legend, ax, **kwargs)
253
254 # Pad the sides of the plot only when not interpolating
--> 255 ax.set_xlim(x.min(), x.max())
256 x_diff = x[1] - x[0]
257 if not interpolate:
UnboundLocalError: local variable 'x' referenced before assignment
tsplot 的语法是:
sns.tsplot(data, time=None, unit=None, condition=None, value=None, err_style='ci_band', ci=68, interpolate=True, color=None, estimator=<function mean at 0x00000000044F2C18>, n_boot=5000, err_palette=None, err_kws=None, legend=True, ax=None, **kwargs)
所以我将索引作为时间参数提供我的数据,但我不确定我做错了什么。我认为我不需要任何其他关键字参数,但也许这就是问题所在。
如果我使用具有维度(单位,时间)的数组来代替:
sns.tsplot(data.values.T, time=idx)
我得到了预期的输出(除了没有时间戳是 xlabels):
但是使用数据框执行此操作的正确方法是什么?我知道它必须是“长格式”,但我不太确定这对这个特定框架意味着什么。