arka roy
arka roy

Reputation: 23

Bokeh not displaying plot for pandas

I can't get Bokeh to display my plot. This is my Python code.

import pandas as pd
from bokeh.plotting import figure, ColumnDataSource
from bokeh.io import output_file, show


if __name__ == '__main__':
    file = 'Overview Data.csv'
    overview_df = pd.read_csv(file)
    overview_ds = ColumnDataSource(overview_df)
    output_file('Wins across Seasons.html')
    print(overview_ds.data)
    p = figure(plot_width=400, plot_height=400)

    # add a circle renderer with a size, color, and alpha
    p.circle('Season', 'Wins', source = overview_ds, size=20, color="navy", alpha=0.5)

    # show the results
    show(p)

I checked my Chrome browser Inspect Element and the console shows the following.

Wins across Seasons.html:17 [bokeh] could not set initial ranges e.set_initial_range @ Wins across Seasons.html:17

This only seems to happen when I am reading from a file. Hard-coding x and y coordinates work.

I have checked other posts but none of the fixes worked. All my packages are up to date.

This is the file I am reading

Season,Matches Played,Wins,Losses,Goals,Goals Conceded,Clean Sheets
2011-12,38,28,5,89,33,20
2010-11,38,23,4,78,37,15
2009-10,38,27,7,86,28,19
2008-09,38,28,4,68,24,24
2007-08,38,27,5,80,22,21
2006-07,38,28,5,83,27,16

This is the output of the print statement.

{'Season': array(['2011-12', '2010-11', '2009-10', '2008-09', '2007-08', '2006-07'],
      dtype=object), 'Matches Played': array([38, 38, 38, 38, 38, 38], dtype=int64), 'Wins': array([28, 23, 27, 28, 27, 28], dtype=int64), 'Losses': array([5, 4, 7, 4, 5, 5], dtype=int64), 'Goals': array([89, 78, 86, 68, 80, 83], dtype=int64), 'Goals Conceded': array([33, 37, 28, 24, 22, 27], dtype=int64), 'Clean Sheets': array([20, 15, 19, 24, 21, 16], dtype=int64), 'index': array([0, 1, 2, 3, 4, 5], dtype=int64)}

Upvotes: 2

Views: 3524

Answers (2)

habet
habet

Reputation: 146

you can convert the 'Season'-column to datetime to get an output.

overview_df = pd.read_csv(file)
overview_df.Season = pd.to_datetime(overview_df.Season)
overview_ds = ColumnDataSource(overview_df)

Upvotes: 1

bigreddot
bigreddot

Reputation: 34628

Bokeh does not know what to do with those string dates unless you tell it. There are two basic possibilities:

  • Keep them as strings, and treat them as categorical factors. You can do that by telling Bokeh what the factors are when you create the plot:

    p = figure(plot_width=400, plot_height=400, 
               x_range=list(overview_df.Season.unique()))
    

    That results in this figure:

    enter image description here

    If you want a different order of categories you can re-order x_range however you like.

  • Convert them to real datetime values and use a datetime axis. You can do this by telling Pandas to parse column 0 as a date field:

    overview_df = pd.read_csv(file, parse_dates=[0])
    

    and telling Bokeh to use a datetime axis:

    p = figure(plot_width=400, plot_height=400, x_axis_type="datetime")
    

    That results in this figure:

    enter image description here

Upvotes: 4

Related Questions