由于与ColumnDataSource不兼容而导致运行时错误

时间:2018-03-19 16:27:08

标签: python python-3.x bokeh glyph

以下是使用CDS(列数据结构)时出错的代码。

这是我得到的错误: RuntimeError screenshot

有什么想法吗?

#Plotting flower species

#Importing libraries
from bokeh.plotting import figure
from bokeh.io import output_file, show
from bokeh.sampledata.iris import flowers
from bokeh.models import Range1d, PanTool, ResetTool, HoverTool, ColumnDataSource, LabelSet

colormap={'setosa':'red','versicolor':'green','virginica':'blue'}
flowers['color']=[colormap[x] for x in flowers['species']]

setosa=ColumnDataSource(flowers[flowers["species"]=="setosa"])
versicolor=ColumnDataSource(flowers[flowers["species"]=="versicolor"])
virginica=ColumnDataSource(flowers[flowers["species"]=="virginica"])

#Define the output file path
output_file("iris.html")

#Create the figure object
f=figure()

#adding glyphs
f.circle(x="petal_length", y="petal_width",
     size=[i*4 for i in setosa.data["sepal_width"]],
     fill_alpha=0.2,color="color",line_dash=[5,3],legend='Setosa',source=setosa)

f.circle(x="petal_length", y="petal_width",
     size=[i*4 for i in setosa.data["sepal_width"]],
     fill_alpha=0.2,color="color",line_dash=[5,3],legend='Versicolor',source=versicolor)

f.circle(x="petal_length", y="petal_width",
     size=[i*4 for i in setosa.data["sepal_width"]],
     fill_alpha=0.2,color="color",line_dash=[5,3],legend='Virginica',source=virginica)

#Save and show the figure
show(f)

1 个答案:

答案 0 :(得分:1)

您需要将size列放在数据框中:

flowers['size'] = [i*4 for i in flowers["sepal_width"]]

所以它在你后来制作的ColumnDataSource中。然后使用带有字形函数的列名:

f.circle(x="petal_length", y="petal_width", size="size", color="color",
         fill_alpha=0.2, line_dash=[5,3],legend='Setosa', source=setosa)

但是,您也可以只传递DataFrames,并自动为您创建CDS,这更简单。这是一个完整的版本:

#Plotting flower species

#Importing libraries
from bokeh.plotting import figure
from bokeh.io import output_file, show
from bokeh.sampledata.iris import flowers

colormap={'setosa':'red', 'versicolor':'green', 'virginica':'blue'}
flowers['color'] = [colormap[x] for x in flowers['species']]
flowers['size'] = [i*4 for i in flowers["sepal_width"]]

setosa = flowers[flowers["species"]=="setosa"]
versicolor = flowers[flowers["species"]=="versicolor"]
virginica = flowers[flowers["species"]=="virginica"]

#Define the output file path
output_file("iris.html")

#Create the figure object
f=figure()

#adding glyphs
f.circle(x="petal_length", y="petal_width", size="size", color="color",
     fill_alpha=0.2,line_dash=[5,3], legend='Setosa', source=setosa)

f.circle(x="petal_length", y="petal_width", size="size", color="color",
     fill_alpha=0.2,line_dash=[5,3],legend='Versicolor', source=versicolor)

f.circle(x="petal_length", y="petal_width", size="size", color="color",
     fill_alpha=0.2,line_dash=[5,3],legend='Virginica', source=virginica)

#Save and show the figure
show(f)

enter image description here