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

2024-06-28 20:48:13 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是使用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)

Tags: infromimportforbokehwidthcolorfigure
1条回答
网友
1楼 · 发布于 2024-06-28 20:48:13

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

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

所以它是在ColumnDataSource中,你稍后再做。然后将列名与glyph函数一起使用:

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

不过,您也可以只传递数据帧,然后会自动为您创建一张CD,这更简单。以下是完整版本:

#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

相关问题 更多 >