Reputation: 21
I'm reading a .csv file in bokeh which has two columns: one for date and one for the values corresponding to that date. I'm trying to make a line graph with the dates on the x axis and the values on y, but it isn't working. Any ideas?
CODE:
import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource
from datetime import datetime
from bokeh.palettes import Spectral3
output_file('output.html')
df = pd.read_csv('speedgraphak29.csv')
p = figure(x_axis_type="datetime")
p.line(x=df.dates, y=df.windspeed, line_width=2)
show(p)
It's returning an empty graph. What should I do?
Upvotes: 2
Views: 6435
Reputation: 190
You could use this. Say you have a CSV called sample_data.csv
with columns Date
and Amount
. Just to add on to what Jasper had.
import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource
output_file('output.html')
df = pd.read_csv('sample_data.csv', parse_dates=['Date'])
source = ColumnDataSource(df)
p = figure(x_axis_type="datetime")
p.line(x='Date', y='Amount', line_width=2, source=source)
show(p)
In this case, read the CSV with the column as a date format. Using ColumnDataSource
allows you to use advanced features like hovering over a plot to see more details if needed.
You may alternatively also use lists directly which would look like.
p.line(x='my_list_of_dates', y='my_list_of_counts', line_width=2)
This would mean reading each column and making a list from it. All in all, using ColumnDataSource would allow you to directly call a column by its name.
Upvotes: 2
Reputation: 1795
Since you didn't provide an example of the input data I had to make something up. You probably forgot to specify that the dates column should be interpreted as datetime values as bigreddot noted. Here is a working example:
import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource
from datetime import datetime
from bokeh.palettes import Spectral3
output_file('output.html')
df = pd.DataFrame.from_dict({'dates': ["1-1-2019", "2-1-2019", "3-1-2019", "4-1-2019", "5-1-2019", "6-1-2019", "7-1-2019", "8-1-2019", "9-1-2019", "10-1-2019"], 'windspeed': [10, 15, 20,30 , 25, 5, 15, 30, 35, 25]})
df['dates'] = pd.to_datetime(df['dates'])
source = ColumnDataSource(df)
p = figure(x_axis_type="datetime")
p.line(x='dates', y='windspeed', line_width=2, source=source)
show(p)
Upvotes: 2