简体   繁体   English

使用散景创建条形图

[英]using bokeh to create a bar graph

there is an example on the bokeh website:散景网站上有一个示例:

https://docs.bokeh.org/en/latest/docs/gallery/bar_nested.html https://docs.bokeh.org/en/latest/docs/gallery/bar_nested.html

but it does not work on my Jupiter notebook.但它不适用于我的 Jupiter 笔记本。

I have the following data frame:我有以下数据框:

                precision   recall  f1
Random Forest   0.493759    1.0     0.661096
XGBoost         0.493759    1.0     0.661096

I want to build a graph that compares the two models on these 3 metrics.我想构建一个图表来比较这 3 个指标的两个模型。 But to start, I just wanted to compare one metric.但首先,我只想比较一个指标。 this is my non-working code:这是我的非工作代码:

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure

data = pd.DataFrame({'precision':[percision_rf,percision_xgb],'recall':[recall_rf,recall_xgb],'f1':[f1_rf,f1_xgb]})
data.rename({0:'Random Forest',1:'XGBoost'}, inplace=True)

source = ColumnDataSource(data=data)

p = figure()

p.vbar(x='Random Forest', top=0.9, width=0.9, source=source)

p.y_range.start = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None

show(p) 

There is an example of a simple bar graph on the bokeh website, but it is not using a ColumnDataSource.散景网站上有一个简单的条形图示例,但它没有使用 ColumnDataSource。

When you pass a DataFrame to a ColumnDataSource , Bokeh makes CDS columns out of the columns of the DataFrame.当您将DataFrame传递给ColumnDataSource ,Bokeh 从 DataFrame 的列中生成 CDS 列。 Those are what you can refer to in the glyph methods, and then the glyph will draw glyphs for all values of that column.这些是您可以在字形方法中引用的内容,然后字形将为该列的所有值绘制字形。 For example, in the example above, you could do例如,在上面的例子中,你可以这样做

# plot bars for every precision value along the x axis
p.vbar(x='precision', top=0.9, width=0.9, source=source)

All Bokeh glyphs are inherently "vectorized" in this way.所有散景字形都以这种方式固有地“矢量化”。

In the above code, x='Random Forest' is not meaningful to pass to vbar , because there is no column in the DataFrame (and hence no column in the CDS) called "Random Forest".在上面的代码中, x='Random Forest'传递给vbar没有意义,因为DataFrame没有名为“Random Forest”的列(因此 CDS 中也没有列)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM