首页 > 解决方案 > 无法从“bokeh.plotting”导入名称“Scatter”

问题描述

我正在尝试使用散景散点图来表示数据。这是我的代码:

from bokeh.plotting import Scatter, output_file, show import pandas

df=pandas.Dataframe(colume["X","Y"])

df["X"]=[1,2,3,4,5,6,7]
df["Y"]=[23,43,32,12,34,54,33]

p=Scatter(df,x="X",y="Y", title="Day Temperature measurement", xlabel="Tempetature", ylabel="Day")
output_file("File.html")
show(p)

输出应如下所示: 预期输出

错误是:

ImportError                               Traceback (most recent call
> last) <ipython-input-14-1730ac6ad003> in <module>
> ----> 1 from bokeh.plotting import Scatter, output_file, show
>       2 import pandas
>       3 
>       4 df=pandas.Dataframe(colume["X","Y"])
>       5 

ImportError: 无法从 'bokeh.plotting' 导入名称 'Scatter' (C:\Users\LENOVO\Anaconda3\lib\site-packages\bokeh\plotting__init__.py)

我还发现 Scatter 现在不再维护。有什么方法可以使用它吗?另外,我必须使用任何其他 python 库来表示与 Scatter 相同的数据?

使用旧版本的 Bokeh 会解决这个问题吗?

标签: pythonpandasbokehscatter

解决方案


如果您在文档中查找“scatter”,您会发现

分散标记

要在绘图上散布圆形标记,请使用circle()Figure 的方法:

from bokeh.plotting import figure, output_file, show

# output to static HTML file
output_file("line.html")

p = figure(plot_width=400, plot_height=400)

# add a circle renderer with a size, color, and alpha
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)

# show the results
show(p)

要使用数据框,只需将和之类的列传df.X递给df.Yxargs y

from bokeh.plotting import figure, show, output_file
import pandas as pd

df = pd.DataFrame(columns=["X","Y"])

df["X"] = [1,2,3,4,5,6,7]
df["Y"] = [23,43,32,12,34,54,33]

p = figure()
p.scatter(df.X, df.Y, marker="circle")

#from bokeh.io import output_notebook
#output_notebook()

show(p)  # or output to a file...

推荐阅读