Reputation: 56
I'm having trouble understanding how datetime works in Bokeh.
My code is outputting milliseconds on the x axis, which seems to be the default if the axis type is 'datetime', but I would like to have it separated by every 5 years.
How can I accomplish this in Bokeh? The dates are before 1950 so I can't use a timestamp. When I exclude the datetime axis type, it displays every single value as a tick on the x axis. When I try to change the inputted data as a datetime object, Bokeh rejects it.
I've read other posts on the subject, as well as Bokeh's documentation on DatetimeTickFormatter, which doesn't seem to be working for me either.
Bokeh FixedTicker with Custom Datetime/Timestamp values
All my code for the plot is below:
r = requests.get('https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json')
data = json.loads(r.text)
chart_data = {
'date': [a[0] for a in data['data']], #this is the problem data
'gdp': [a[1] for a in data['data']],
}
source = ColumnDataSource(chart_data)
hover = HoverTool(
tooltips=[
('GDP', '@gdp{$0,0.00}'),
('Date', '@date{%Y - %B}'),
],
formatters={
'date' : 'datetime',
}
)
bar = figure(x_range=chart_data['date'],
x_axis_type='datetime',
plot_height=750,
plot_width=1000,
tools='wheel_zoom, reset, save',
title=data['source_name'])
bar.vbar(x='date', top='gdp', width=0.9, source=source)
bar.add_tools(hover)
#visual settings
bar.xaxis.formatter = DatetimeTickFormatter(years = ['%Y'])
bar.xaxis.major_label_orientation = 'vertical'
bar.xaxis.minor_tick_line_color = None
bar.xgrid.grid_line_color = None
bar.yaxis.formatter = NumeralTickFormatter(format='$0,0.00')
bar.ygrid.grid_line_color = None
show(bar)
Thank you for the help, this has been driving me crazy!
Upvotes: 0
Views: 6045
Reputation: 56
Thanks to help from bigreddot. My issue was that my plot was acting as a Categorical range due to me setting the x_range property, instead of defaulting to datetime.
Upvotes: 1