user10862252
user10862252

Reputation:

Bokeh ColumnDataSource line not rendering

I have a SCV with Values over Time and Date but the bokeh line won't render.

I followed this tutorial (https://programminghistorian.org/en/lessons/visualizing-with-bokeh) to the point of showing the first scatterplot to get a bit familiar with bokeh and pandas since I'm quite new to Python and programming in general. This worked fine in my jupyter notebook.

So I wanted to try this out with my own data, witch is a count of stream listeners over time. After figuring out how to properly import the csv into a pandas.DataFrame, I had to fiddle around with the data in the columns "Date" and "Time" to get the string to a datetime64 value.

The data now looks like this:

         Date      Time  Count
0      01.01.2012  00:00:00     69
1      01.01.2012  00:01:00     65
2      01.01.2012  00:02:00     65
3      01.01.2012  00:03:00     65

But if I feed "Date" or "Time" together with "Count" into the bokeh plot I get an empty plot with no line. Axis labels and title get rendered correctly.

I checked in different browsers to make sure it was not a browser issue. I also tried to not use a ColumnDataSource but instead use lists which gave the same result. But if I defined two short lists like this

x = [1, 2, 3, 4]
y = [1, 2, 3, 4]

and use those instead of "Time" or "Date" the line renders like expected.

I tried to find some clarification in the pandas and bokeh documentation on how to use ColumnDataFrame and the line glyph but to me it seems like the graph should render.

I have a suspicion that maybe the raw data has some flaws which I can't spot? On looking over the .csv I can't find anything like missing entries.

The code looks like this:

import pandas
from bokeh.plotting import figure, show
from bokeh.io import output_notebook, output_file
from bokeh.models import ColumnDataSource
output_notebook()

data = pandas.read_csv('listener.csv', delimiter=",", usecols= [1, 2, 3], names=["Date", "Time", "Count"])

data["Date"].astype("datetime64")
data["Time"].astype("datetime64")

source = ColumnDataSource(data)
p = figure(title="Title", x_axis_label="Date",x_axis_type="datetime", y_axis_label="Listener")
p.line(x="Time", y="Count", source=source, line_width=2)
output_file("plot.html")

show(p)

Since in the above mentioned tutorial everything worked, which uses basically the same code, I expected to get a nice line over time or date depending on the input. But what I get is an empty plot.

Upvotes: 0

Views: 1465

Answers (1)

user10862252
user10862252

Reputation:

Thanks to the help of Josh Friedlander I could solve my problem and some of those I had while experimenting. So here is my solution in wich I used the parse_dates to combine the colums "Date" and "Time" into one column wich has the datatyp "datetime64". dayfirst was also used because of european date formating.

data = pandas.read_csv(
        "filename",
        delimiter=",",
        usecols=[1, 2, 3],
        names=["Date", "Time", "Count"],
        parse_dates=[["Date", "Time"]], 
        dayfirst=True)

This makes obsolet.

data["Date"].astype("datetime64")
data["Time"].astype("datetime64")

Upvotes: 1

Related Questions