我正在尝试使用 Bokeh 散点来表示数据。 这是我的代码:
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)
输出应如下所示: Expected Output
错误是:
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: cannot import name 'Scatter' from 'bokeh.plotting' (C:\Users\LENOVO\Anaconda3\lib\site-packages\bokeh\plotting__init__.py)
我还发现分散现在不再维护了。有什么办法可以使用吗? 另外,我必须使用哪种替代方法来使用任何其他 python 库来表示与 Scatter 相同的数据?
使用旧版本的 Bokeh 可以解决此问题吗?
最佳答案
Scatter
(大写 S)从未成为 bokeh.plotting
的一部分。它曾经是旧的 bokeh.charts
API 的一部分,几年前已被删除。但是,根本不需要创建基本的散点图,因为 bokeh.plotting
中的所有字形方法(例如 circle
、square
)都是隐式分散类型函数:
from bokeh.plotting import figure, show
import pandas as pd
df = pd.DataFrame({"X" :[1,2,3,4,5,6,7],
"Y": [23,43,32,12,34,54,33]})
p = figure(x_axis_label="Tempetature", y_axis_label="Day",
title="Day Temperature measurement")
p.circle("X", "Y", size=15, source=df)
show(p)
其产量:
您还可以将数据直接传递到circle
,如 in the other answer 。
如果你想做更奇特的事情,比如 map the marker type based on a column图中还有一个plot.scatter
(小写s)方法:
from bokeh.plotting import figure, show
from bokeh.sampledata.iris import flowers
from bokeh.transform import factor_cmap, factor_mark
SPECIES = ['setosa', 'versicolor', 'virginica']
MARKERS = ['hex', 'circle_x', 'triangle']
p = figure(title = "Iris Morphology")
p.xaxis.axis_label = 'Petal Length'
p.yaxis.axis_label = 'Sepal Width'
p.scatter("petal_length", "sepal_width", source=flowers, legend_field="species", fill_alpha=0.4, size=12,
marker=factor_mark('species', MARKERS, SPECIES),
color=factor_cmap('species', 'Category10_3', SPECIES))
show(p)
产生:
关于python - 无法从 'Scatter' 导入名称 'bokeh.plotting',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59670727/