gpt4 book ai didi

python - UnboundLocalError : local variable 'x' referenced before assignment. 在数据帧的 seaborn 包中正确使用 tsplot?

转载 作者:太空狗 更新时间:2023-10-29 22:22:15 24 4
gpt4 key购买 nike

我无法让它对我的数据起作用,所以首先我尝试了一个非常相似的具体示例。这是数据框:

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)

所以我将索引作为时间参数提供给我的数据,但我不确定我做错了什么。我认为我不需要任何其他关键字参数,但也许这就是问题所在。

如果我改为使用维度为 (unit,time) 的数组来执行此操作:

sns.tsplot(data.values.T, time=idx)

我得到了预期的输出(除了没有时间戳的是 xlabels):

enter image description here

但是使用数据框执行此操作的正确方法是什么?我知道它必须是“长格式”,但我不太确定这对这个特定的框架意味着什么。

最佳答案

我终于明白了。基本上我应该首先看的地方是 here在标题为“使用长格式数据帧指定输入数据”的部分中。我必须做的是:

data.reset_index(inplace=True)
data.columns = np.arange(len(data.columns))
melted = pd.melt(data, id_vars=0)

第一行将 DatetimeIndex 移动到它自己的列中,并就地设置默认整数索引。第二行对标题执行相同的操作,除了将它们删除(我需要这样做,因为似乎无法使用多索引进行分组)。最后,我们融化数据创建如下所示的 DataFrame:

In [120]:

melted
Out[120]:
0 variable value
0 1990-01-01 1 1
1 1990-01-02 1 2
2 1990-01-03 1 3
3 1990-01-04 1 4
4 1990-01-05 1 5
5 1990-01-01 2 6
6 1990-01-02 2 7
7 1990-01-03 2 8
8 1990-01-04 2 9
9 1990-01-05 2 1
10 1990-01-01 3 2
11 1990-01-02 3 3
12 1990-01-03 3 4
13 1990-01-04 3 5
14 1990-01-05 3 6
15 1990-01-01 4 7
16 1990-01-02 4 8
17 1990-01-03 4 9
18 1990-01-04 4 1
19 1990-01-05 4 2

现在,在 DataFrame 准备就绪后,我可以像这样使用 tsplot:

sns.tsplot(melted, time=0, unit='variable', value='value')

在我的情况下,这与我所做的几乎相同:

sns.tsplot(data.T.values, idx)
plt.xlabel('0')
plt.ylabel('value')

除非我添加任何条件,否则 tsplot 会绘制其他系列并为我制作图例。

鉴于函数的性质,tsplot 至少可以将日期绘制为时间戳就好了。我认为使用转置数组对于我的应用程序来说比直接使用 DataFrame 更容易。

关于python - UnboundLocalError : local variable 'x' referenced before assignment. 在数据帧的 seaborn 包中正确使用 tsplot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27310500/

24 4 0
文章推荐: Python mysql.connector cursor.lastrowid 总是返回 0
文章推荐: c# - 如何在 C# 中比较两个 list 并只保留没有重复的项目?