- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我无法让它对我的数据起作用,所以首先我尝试了一个非常相似的具体示例。这是数据框:
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):
但是使用数据框执行此操作的正确方法是什么?我知道它必须是“长格式”,但我不太确定这对这个特定的框架意味着什么。
最佳答案
我终于明白了。基本上我应该首先看的地方是 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/
如何从 seaborn 生成的热图中隐藏颜色条 import numpy as np; np.random.seed(0) import seaborn as sns; sns.set_theme()
我正在尝试使用 seaborn 制作热图,但被困在更改特定值的颜色。假设值 0 应该是白色,值 1 应该是灰色,然后使用 cmap 提供的调色板。 试图使用面具,但感到困惑。 import matpl
我想改变散点的大小。 这些都不起作用: sns.relplot(x='columnx', y='columny', hue='cluster', data=df) sns.relplot(x='col
这个问题在这里已经有了答案: What is y axis in seaborn distplot? (3 个答案) 关闭 3 年前。 我正在使用以下语句绘制分布图: a = sns.distplo
我注意到 sns.barplot 使用标准错误作为误差条默认 1 。有办法把它改成SD吗? ax = sns.barplot(x="day", y="tip", data=tips, ci=???)
向 seaborn FacetGrid 中的每个直方图添加表示平均值(或其他集中趋势度量)的点和可变性度量(例如,标准偏差或置信区间)的最佳方法是什么? 结果应该类似于显示的图 here ,但在每个
我正在尝试使用 sns.histplot() 而不是 sns.distplot() 因为我在 colab 中收到以下消息: FutureWarning: distplot is a deprecate
我想绘制 3 个水平条形图,标签作为 y 轴,数据作为 x 轴,我希望每个图都是不同的颜色,并有某种类型的注释,例如星号,这取决于关于数据中某列所表示的重要性,例如: dat = pd.DataFra
根据 seaborn 文档 here seaborn.distplot()已被弃用,向前支持的图是:seaborn.displot()和 seaborn.histplot() . 但是,当我尝试使用
为了使 seaborn.pairplot() 正常工作,在 jupyter notebook 中执行了以下步骤。 /usr/local/lib/python2.7/site-packages/matp
使用 pandas 数据框绘制混淆矩阵时 y 轴两端被切一半? 这就是我得到的: 我使用了这里的代码How can I plot a confusion matrix?使用 pandas 数据框: i
您好,我刚刚为 seaborn 热图创建了自定义 cmap,但是当我想使用它时,它没有显示正确的颜色。我已经一步一步完成了: import seaborn as sns import numpy as
亲爱的,我正在尝试将 kaggle 教程代码应用于 Iris 数据集。 不幸的是,当我执行图表的代码时,我只能看到这个输出而看不到任何图表: matplotlib.axes._subplots.Axe
这个问题在这里已经有了答案: Seaborn plots in a loop (6 个答案) How to plot in multiple subplots (12 个答案) 关闭 1 年前。 我
我正在尝试在 python 中使用 seaborn 绘制直方图。但它给我的只是一个空白数字。 这是我专栏的describe(): 代码: plt.subplots(figsize=(7,7)) sns
如何在seaborn.lineplot中分别设置标记和线条的透明度? 我有一组点,我想画一条连接所有点的线图。我希望线条比标记更透明。如何做到这一点? 这是我的目标: 这是我的代码: import m
我正在使用 seaborn 库在 python 中绘制热图。数据框包含一些缺失值 (NaN)。我希望与这些字段对应的热图单元格是白色的(默认情况下)并且还用字符串 NA 进行注释。但是,如果我看对了,
如何对这个图进行排序以从大到小显示?我尝试使用 sort_values 但不起作用 plt.figure(figsize=(15,8)) sns.countplot(x='arrival_date_m
我的目标是在使用 seaborn 绘制的图上的 y = 0 上绘制一条水平红线:sns.lmplot由 col= 分割或 row= . import numpy as np, seaborn as s
我正在使用seaborn pairplot绘制我的数据点不同维度的散点图。但是,我希望数据点的标记具有与数据点的维度之一相对应的大小。我有以下代码: markersize = 1000* my_dat
我是一名优秀的程序员,十分优秀!