Python 由于与ColumnDataSource不兼容而导致运行时错误
下面是使用CDS(列数据结构)时出错的代码 以下是我得到的错误: 有什么想法吗Python 由于与ColumnDataSource不兼容而导致运行时错误,python,python-3.x,bokeh,glyph,Python,Python 3.x,Bokeh,Glyph,下面是使用CDS(列数据结构)时出错的代码 以下是我得到的错误: 有什么想法吗 #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, Rese
#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)
您需要将
size
列放入数据框中:
flowers['size'] = [i*4 for i in flowers["sepal_width"]]
因此,您稍后将在列datasource
中创建它。然后将列名与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)
非常感谢@bigreddot我对这些CD感到困惑,有了你的解释,我现在明白了,没有制作尺寸我错过了什么…我很高兴这有帮助!如果你能投票/打分正确,那将有助于其他人找到答案。