Search code examples
pythonpandasbokeh

Bokeh Library (Python) Issue: "Failed to auto-convert {curr_type} to ColumnDataSource."


I am having a problem trying to show a simple plot graph with results from 3 teams, it can't convert to ColumnDataSource, your help is appreciated!!

I am new to Bokeh, and Python in general, and could use some tips if this code has some serious issues!

#importing libraries
from bokeh.plotting import figure
from bokeh.io import output_file, show
import pandas as pd
from bokeh.models import ColumnDataSource

# initialize list of lists 
data = [['HR', 68, 76], ['MA', 69, 72], ['FI', 70, 74]] 

# Create the pandas DataFrame 
dfTeam = pd.DataFrame(data, columns = ['name', 'current_score', 'previous_score']) 


# assign a color code
colormap={'HR':'red', 'MA':'green','FI':'blue'}
dfTeam['color']=[colormap[x] for x in dfTeam['name']]

HRsrc=ColumnDataSource(dfTeam[dfTeam['name']=='HR'])
MAsrc=ColumnDataSource(dfTeam[dfTeam['name']=='MA'])
FIsrc=ColumnDataSource(dfTeam[dfTeam['name']=='FI'])


####define the output file path
output_file('test.html')

###create figure object
f=figure()

###adding glyphs
f.circle(x='current_score', y='previous_score',
         fill_alpha=0.2, color='color', legend='HR', source='HRsrc')

f.circle(x='current_score', y='previous_score',
         fill_alpha=0.2, color='color', legend='MA', source='MAsrc')

f.circle(x='current_score', y='previous_score',
         fill_alpha=0.2, color='color', legend='FI', source='FIsrc')

##save and show the figure
show(f)

Solution

  • Welcome to Stackoverflow!

    bokeh's columndatasource is an object wrapper. From the docs:

    At the most basic level, a ColumnDataSource is simply a mapping between column names and lists of data. The ColumnDataSource takes a data parameter which is a dict, with string column names as keys and lists (or arrays) of data values as values.

    In your case, you've put quotes around source='HRsrc') which means that python think's it's a string.

    Remove the quotes around each variable name in source= to get your plot.