Razieh Delavar
Razieh Delavar

Reputation: 9

Bokeh plot line

I tried to write these codes to display the dataseries plot, but no data was not displayed. I dont know where is the problem exactly.

data=pd.read_csv('weather.csv')[['STA','Date','Precip','MaxTemp','MinTemp','MeanTemp','Snowfall']].dropna()
data = data[data['Precip'] != 'T']
data['Precip'].astype(float)
data['STA']=data['STA'].astype("string")
data['Date']=pd.to_datetime(data['Date'])
stations=list(set(data['STA']))
stations.sort()
select_inital=select.value
colors = list(Category20_16)
colors.sort()
subset=data[data['STA']==select_inital]
initial_values= list(set(subset['STA']))
for i, j in enumerate(initial_values):
   subset=data[data['STA']==j]
   d=subset[['Date','Precip']]
   d.sort_values('Date')
   x=d['Date']
   y=d['Precip']
   d = ColumnDataSource(d)

p = figure(plot_width=700, plot_height=700, x_range=(0,200), title='Weather      Evolution',x_axis_label='Date', y_axis_label='Precip',x_axis_type='datetime')
p.line(x,y, legend_label="Evolution", line_width=2)
show(p)

Upvotes: 0

Views: 339

Answers (1)

mosc9575
mosc9575

Reputation: 6337

This is just guessing but I believe the problem is, that you are trying to set limits to the x_range. Bokeh is evaluating dates as milliseconds from 1970-01-01 00:00 and your x_range=(0,200) is also interpreted as millisecond. This means the visible area is very small and starts at January 1st 1970. You could use the defaults by bokeh instead.

Minimal example

This is your code for the figure except I removed the x_range.

import pandas as pd
from bokeh.plotting import figure, show, output_notebook
output_notebook()

x = pd.date_range('2022-12-01', '2022-12-24', freq='D')
y = list(range(1,25))

p = figure(
    plot_width=700,
    plot_height=700,
    # x_range=(0,200),
    title='Weather      Evolution',
    x_axis_label='Date',
    y_axis_label='Precip',
    x_axis_type='datetime'
)
p.line(x,y, legend_label="Evolution", line_width=2)
show(p)
Bokeh default x_range x_range by user
with default range with set x_range

Comment

If you want to set the x_range for a axis with type "datetime" you can pass timestamp objects to it.

Valid are among other things (e.g. float)

# datetime
from datetime import datetime
x_range=(datetime(2022,12, 7),datetime(2022,12, 10))

# pandas
import pandas as pd
x_range=(pd.Timestamp('2022-12-07'),pd.Timestamp('2022-12-10'))

Upvotes: 1

Related Questions