Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"BAD_COLUMN_NAME" message from Bokeh plot

Tags:

bokeh

I am trying to create a simple bokeh chart (vert or hor) from a csv file and seem to be having issues. I am able to create the chart utilizing ColumnDataSource by listing items manually, but when I try to create the same chart from a simple csv file, it seems to give me trouble. Now I am trying to just create the figure by reading a pandas df. I am getting the dreaded Bad Column Name error message. Any help is appreciated. First time posting so let me know if I have posted incorrectly and I will fix. Thanks in advance.

from bokeh.io import output_notebook, show
output_notebook()

from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, 
HoverTool, FactorRange, Range1d
from bokeh.plotting import figure
from bokeh.transform import dodge

output_file("test.html")

import pandas as pd

df = pd.read_csv(r'C:\test.csv')

print(df)

Mains  Total Length  Length Surveyed

0 1.0 88.4 87.6 1 2.0 313.8 316.8 2 3.0 271.0 265.6 3 4.0 155.0 153.1 4 5.0 301.8 299.0 5 6.0 293.9 132.3 6 7.0 148.1 147.2 7 8.0 292.9 290.1 8 9.0 307.6 306.0 9 10.0 559.0 236.0 10 11.0 448.8 441.5 11 12.0 297.9 13.0 12 13.0 172.2 67.5

source = ColumnDataSource(data=dict(df))
Mains = data=dict(df)
data = {'Mains': df}

p = figure(x_range=(0,20), y_range=(0, 500), 
    plot_height=250, title="CCTV Survey August 6- 
    9th, 
    2018",
    toolbar_location=None, tools="")

p.vbar(x=dodge('Mains', -0.25, range=p.x_range), 
    top='2015', width=0.2, source=source,
    color="#c9d9d3", legend=value("Total 
    Length"))

p.vbar(x=dodge('Mains',  0.0,  range=p.x_range), 
    top='2016', width=0.2, source=source,
    color="#718dbf", legend=value("Length 
    Surveyed"))

p.add_tools(HoverTool(tooltips=[("Total Length", 
    "@2015 ft"),("Length Surveyed", "@2016 
    ft")]))
p.xaxis.major_label_orientation = 1.4
##p.x_range.factors=data_dict['x']
##p.x_range.range_padding = 0.0
p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"

show(p)

ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: 2015 [renderer: GlyphRenderer(id='107d32b5-2700-4608-b1d8-9d0602f82a5b', ...)] ERROR:bokeh.core.validation.check:E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: 2016 [renderer: GlyphRenderer(id='4dcb960b-b29e-4998-972a-046311d037f8', ...)]

like image 660
dbach75 Avatar asked Nov 07 '22 02:11

dbach75


1 Answers

You are telling Bokeh that the top of the bars should be driven by a column named "2016":

top='2016', width=0.2, source=source,

But your dataframe / column data source has no such column. You need to configure the bar glyphs to use columns that are actually part of your data (presumably "Total Length" or "Length Surveyed")

like image 75
bigreddot Avatar answered Nov 11 '22 16:11

bigreddot