pandas 使用散景中 x 坐标的数据帧索引绘制熊猫数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37904231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plot a pandas dataframe using the dataframe index for x coordinate in bokeh
提问by Krastanov
I want to prepare a bokeh plot that uses a ColumnDataSource
. The pandas
DataFrame
that is the source of the data has one column and a datetime
index:
我想准备一个使用ColumnDataSource
. 的pandas
DataFrame
是数据源具有一个柱和一个datetime
指数:
How do I specify that the x value should be the index. I tried just omitting it, hoping that would be the default, but it did not work:
我如何指定 x 值应该是索引。我试着省略它,希望这是默认值,但它不起作用:
There is an ugly solution where I just copy the index as a column in the dataframe, but I hope there is a more elegant solution:
有一个丑陋的解决方案,我只是将索引复制为数据框中的一列,但我希望有一个更优雅的解决方案:
回答by Luke Canavan
The issue is that you have to specify which column should be the "x" column. If you don't specify the "x" value, the default behavior in bokeh.plotting is to try to find a column called "x" in your ColumnDataSource (which doesn't exist).
问题是您必须指定哪一列应该是“x”列。如果不指定“x”值,bokeh.plotting 中的默认行为是尝试在 ColumnDataSource(不存在)中查找名为“x”的列。
One tricky thing here is that you're using a named index ('timeseries') in pandas. That name is carried over when you create a ColumnDataSource, so that your source probably looks like:
这里的一件棘手的事情是您在 Pandas 中使用了命名索引('timeseries')。创建 ColumnDataSource 时会保留该名称,因此您的源可能如下所示:
ds = ColumnDataSource(df)
print(ds.data)
# the ts_n values would be the actual timestamps from the df
> {'timestamp': [ts_1, ts_2, ts_3, ts_4, ts_5], 'avg': [0.9, 0.8, 0.7, 0.8, 0.9]}
It would work if you use:
如果你使用它会起作用:
p.line(source=ds, x='timestamps', y='avg')
回答by bamdan
I usually reset the index and this makes the index a column. Similar to your ugly solution. Then plot the specified columns.
我通常重置索引,这使索引成为一列。类似于你丑陋的解决方案。然后绘制指定的列。
df.reset_index(inplace = True)
Alternatively you could reference just the column and in matplotlib it usually uses the index by default in the way you want. Not sure if it will work for you but worth a try.
或者,您可以只引用该列,而在 matplotlib 中,它通常以您想要的方式默认使用索引。不确定它是否适合你,但值得一试。
df["avg"].plot()
Alternatively you could try the time series plot approach? Detailed below.
或者,您可以尝试时间序列图方法?下面详解。
回答by fmcmac
You can call the index with the usual syntax to get an index from DF
as:p.line(x = df.index.values, y = df['values_for_y'])
您可以使用通常的语法调用索引以从 DF 获取索引,如下所示:p.line(x = df.index.values, y = df['values_for_y'])