A pandas plot using data index for x coordinate in bokeh

I want to prepare a bokeh site that uses ColumnDataSource. pandas DataFrame, which is a data source, has one column and index datetime:

enter image description here

How to indicate that the value of x should be an index. I just skipped this, hoping it would be the default, but that didn't work:

enter image description here

There is an ugly solution in which I just copy the index as a column in the dataframe, but I hope there is a more elegant solution:

enter image description here enter image description here

+4
source share
3 answers

, , "x" . "x" , bokeh.plotting "x" ColumnDataSource ( ).

, ('timeseries') pandas. 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]}

, :

p.line(source=ds, x='timestamps', y='avg')
+6

reset , . . .

df.reset_index(inplace = True)

, matplotlib , . , , .

df["avg"].plot()

? .

TimeSeries ,

+4

You can call the index using the usual syntax to get the index from DF as:
p.line(x = df.index.values, y = df['values_for_y'])

+2
source

All Articles