How to make the year appear on the x-axis using bokeh

61 views Asked by At

I have a csv file, and is trying to make the year appear on the x-axis, but the year is unable to appear.

from bokeh.plotting import figure
from bokeh.io import output_file, output_notebook, show
from bokeh.layouts import column
from bokeh.models import HoverTool, ColumnDataSource

data = pd.read_csv('amountsaved.csv')
data.head()

data['year']=pd.to_datetime(data['year'])


# create a new plot with a title and axis labels
p = figure(title="amount saved", x_axis_type='datetime', x_axis_label="year", y_axis_label="amount")

# add a line renderer with legend and line thickness
p.line(x, y, legend_label="amt saved", line_width=2)

# Plot date along the x axis and price along the y axis
p.line(data['year'], data['amountsaved'], line_color='green')

# show the results
show(p)

Ooutput:

output

My data in the csv file only has year (eg: 2010,2011,2012) with no month. I'm not sure why the output showed month.

1

There are 1 answers

4
Oskar Hofmann On

Using

data = pd.read_csv('amountsaved.csv', dtype={'year': object})

should solve your problem. Currently, the year column is read as an integer. When you convert an integer to datetime, the number is assumed to be the number of nanoseconds (ns) since 01-01-1970. Your x-axis is in milliseconds (ms), and 2010 ns are basically 0 ms.

Using dtype={'year': object} treats the year column as strings. And when you convert the string '2020' to a datetime, it treats it as a year.