问题
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.
Datetime axis in Bokeh
Bokeh FixedTicker with Custom Datetime/Timestamp values
https://docs.bokeh.org/en/latest/docs/reference/models/formatters.html?highlight=formatters#module-bokeh.models.formatters
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!
回答1:
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.
来源:https://stackoverflow.com/questions/48691083/datetime-on-an-x-axis-in-bokeh