Vlad
Vlad

Reputation: 405

Bokeh ValueError: expected an element of either Seq(String)

I'm trying to build a simple bar chart via bokeh but struggling for it to recognize the x-axis and keep getting a ValueError... I think it needs to be in string format but for some reason whatever I try it just won't work. Please note, the column that contains the Years (as floats by the looks of it) is called RegionName, if it seems confusing. Please see my code below, any suggestions?

import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource
from bokeh.models.tools import HoverTool
import os
from bokeh.palettes import Spectral5
from bokeh.transform import factor_cmap

os.chdir("C:/Users/Vladimir.Tikhnenko/Python/Land Reg")

# Pivot data

def pivot2(infile="Land Registry.csv", outfile="SalesVolume.csv"):
    df=pd.read_csv(infile)
    table=pd.pivot_table(df,index= 
    ["RegionName"],columns="Year",values="SalesVolume",aggfunc=sum)
    table.to_csv(outfile)
    return table
pivot2()

# Transpose data 

df=pd.read_csv("SalesVolume.csv")
df=df.drop(df.columns[1:28],1)
df=pd.read_csv("SalesVolume.csv", index_col=0, header=None).T
df.to_csv("C:\\Users\Vladimir.Tikhnenko\Python\Land 
Reg\SalesVolume.csv",index=None)

df=pd.read_csv("SalesVolume.csv")
source = ColumnDataSource(df)
years = source.data['RegionName'].tolist()
p = figure(x_range=['RegionName'])

color_map = factor_cmap(field_name='RegionName',palette=Spectral5, 
factors=years)

p.vbar(x='RegionName', top='Southwark', source=source, width=1, 
color=color_map)

p.title.text ='Transactions'
p.xaxis.axis_label = 'Years'
p.yaxis.axis_label = 'Number of Sales'

show(p)

the error message is

ValueError: expected an element of either Seq(String), Seq(Tuple(String, 
String)) or Seq(Tuple(String, String, String)), got [1968.0, 1969.0, 1970.0, 
1971.0, 1972.0, 1973.0, 1974.0, 1975.0, 1976.0, 1977.0, 1978.0, 1979.0, 
1980.0, 1981.0, 1982.0, 1983.0, 1984.0, 1985.0, 1986.0, 1987.0, 1988.0, 
1989.0, 1990.0, 1991.0, 1992.0, 1993.0, 1994.0, 1995.0, 1996.0, 1997.0, 
1998.0, 1999.0, 2000.0, 2001.0, 2002.0, 2003.0, 2004.0, 2005.0, 2006.0, 
2007.0, 2008.0, 2009.0, 2010.0, 2011.0, 2012.0, 2013.0, 2014.0, 2015.0, 
2016.0, 2017.0, 2018.0]   

Upvotes: 2

Views: 6166

Answers (1)

bigreddot
bigreddot

Reputation: 34568

Categorical factors must only be strings (or sequences of strings for nested factors), so factor_cmap only accepts lists of those things. You passed it a list a numbers, which causes the error shown. To use use the years as categorical factors, you need to convert them to strings as suggested, and use those string values to initialize x_range, and for the coordinates to vbar.

Alternatively, if you want to use numerical values for the years, but just want to have fixed, controlled tick locations, do this:

p = figure() # don't pass x_range
p.xaxis.ticker = years

And then also use linear_cmap to map the numerical values (instead of factor_cmap)

Upvotes: 5

Related Questions