Paul Shapiro
Paul Shapiro

Reputation: 43

In Bokeh, Weird Date Axis Issue

I'm trying to plot some data with Bokeh via pandas. The x-axis is date, and I can get Bokeh to plot the axis "mostly" correct (the range may be off). However, the line it outputs is all over the place.

For example:

bokeh plot line issue

It looks like maybe it's one big, continuous line?

Here's my code:

# library imports

import pandas as pd
from bokeh.io import output_file, show, vform
from bokeh.plotting import figure, output_file, ColumnDataSource, show
from bokeh.models import HoverTool, BoxAnnotation, BoxSelectTool, BoxZoomTool, WheelZoomTool, ResetTool

# Import csv into pandas dataframe

df = pd.read_csv(r"C:\Users\paul.shapiro\Documents\kwdata.csv", parse_dates=['Interest over time_time'])
df.rename(columns={'Search Term': 'keyword', 'Interest over time_time': 'date', 'Weekly Volume': 'volume'}, inplace=True)

source = ColumnDataSource(data=dict(x=df['date'], y=df['volume'], desc=df['keyword']))
TOOLS = [HoverTool(tooltips=[("Keyword", "@desc"),("Date", "@x"),("Search Volume", "@y")]), BoxZoomTool(), WheelZoomTool(), ResetTool()]

# Output html for embedding
output_file("line.html")

p = figure(plot_width=800, plot_height=800, tools=TOOLS, x_axis_type="datetime")

# add both a line and circles on the same plot
p.line(df['date'], df['volume'], line_width=2, color=df['keyword'], source=source)
p.circle(df['date'], df['volume'], fill_color="white", size=8, source=source)

show(p)

It's also interesting to note, that if you plot it using bokeh.charts (if I did this the tooltips wouldn't work, so it's not an option), it plots fine:

using bokeh.charts instead

defaults.width = 800
defaults.height = 800

TOOLS = [BoxZoomTool(), WheelZoomTool(), ResetTool()]

line = Line(df, x='date', y='volume', color='keyword', source=source, tools=TOOLS)
show(line)
output_file("line.html", title="Search Volume")

Any help would be much appreciated. This has been driving me crazy!


SOLVED using multi_line() and a for loop:

import pandas as pd
from bokeh.io import output_file, show, vform
from bokeh.plotting import figure, output_file, ColumnDataSource, show
from bokeh.models import HoverTool, BoxAnnotation, BoxSelectTool, BoxZoomTool, WheelZoomTool, ResetTool

df = pd.read_csv(r"C:\Users\paul.shapiro\Documents\kwdata.csv", parse_dates=['Interest over time_time'])
df.rename(columns={'Search Term': 'keyword', 'Interest over time_time': 'date', 'Weekly Volume': 'volume'}, inplace=True)

gp = df.groupby('volume')

source = ColumnDataSource(data=dict(x=df['date'], y=df['volume'], desc=df['keyword']))
TOOLS = [HoverTool(tooltips=[("Keyword", "@desc"),("Date", "@x"),("Search Volume", "@y")]), BoxZoomTool(), WheelZoomTool(), ResetTool()]

p = figure(plot_width=800, plot_height=800, tools=TOOLS, x_axis_type="datetime")

gp = df.groupby('keyword')
# groups() returns a dict with 'Gene':indices as k:v pair
for g in gp.groups.items():
    p.multi_line(xs=[df.loc[g[1], 'date']], ys=[df.loc[g[1], 'volume']])

p.circle(df['date'], df['volume'], fill_color="white", size=8, source=source)

output_file("newline.html")
show(p)

Upvotes: 2

Views: 933

Answers (1)

ciacicode
ciacicode

Reputation: 708

I cannot see anything wrong with your code. Try to see how different the dataframe df is from a simple nested list of values as per the bokeh example. Maybe by doing some manipulation to the dataframe you can get this working.

http://docs.bokeh.org/en/latest/docs/reference/plotting.html

from bokeh.plotting import figure, output_file, show

p = figure(plot_width=300, plot_height=300)
p.multi_line(xs=[[1, 2, 3], [2, 3, 4]], ys=[[6, 7, 2], [4, 5, 7]],
         color=['red','green'])

show(p)

Upvotes: 1

Related Questions