我正在尝试使用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:无法从'bokeh.plotting'导入名称'Scatter'(C:\ Users \ LENOVO \ Anaconda3 \ lib \ site-packages \ bokeh \ plotting__init __。py)
我还发现散布现在不再维护。有没有办法使用它?另外,我还必须使用其他任何python库来代表与散点图相同的数据吗?
使用较早版本的Bokeh将解决此问题吗?
如果您在文档中查找“散点图”,则会发现
Scatter Markers
要在绘图上散布圆形标记,请使用图的
circle()
方法: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.Y
之类的列传递给x
和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...
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)
产生: