【问题标题】:Difference between Scatter object and figure.scatter methodScatter 对象和 figure.scatter 方法的区别
【发布时间】:2017-12-18 01:33:30
【问题描述】:

我有以下与这两个图表相关的问题:

图表 1:

output_notebook()
scatter = Scatter(df_b, x='log_umsatz', y='log_fte', color='target', legend="top_right")
show(scatter)

图表 2

output_notebook()
scatter = figure(plot_width=500, plot_height=500)
scatter.scatter(x=df_b['log_umsatz'], y=df_b['log_fte'], color=df['target'])
p.legend.location = "top_left"
p.legend.click_policy="hide"
show(scatter)

如您所见,我使用散景生成了两个散点图。在第二张图中,我尝试引入与p.legend.click_policy="hide" 的一些交互性。我有两个问题:交互性不起作用,并且在第二个示例中丢失了图例和颜色编码。怎么会?我希望图 1 和图 2 是相同的。

【问题讨论】:

    标签: python-3.x bokeh interactive


    【解决方案1】:

    您的主要问题是您使用的是 Graph 1 Scatter,这是一个散景图模型。 Bokeh Charts 是一个用于绘制数据的高级库,并在幕后为您进行大量数据处理和图表格式化。在图 2 中,您使用 Bokeh 字形来创建绘图,因此您需要更加明确地说明您想要它做什么。

    修复您的代码,我可以生成与原始 Scatter 相同的图表。

    cds = ColumnDataSource(df_b)
    color_mapper = CategoricalColorMapper(
        palette=['red', 'green'], factors=[0, 1])
    
    scatter = figure(plot_width=500, plot_height=500)
    scatter.circle(x='log_umsatz', y='log_fte',
                   color={'field': 'target', 'transform': color_mapper}, alpha=0.5,
                   source=cds, legend='target')
    scatter.legend.location = "top_right"
    

    如您所见,我们需要调用多个其他 Bokeh 对象。 ColumnDataSource 存储熊猫数据,CategoricalColorMapper 将颜色映射到因子。


    现在向情节添加交互式图例有点复杂。现在在 Bokeh 交互式图例上是按字形工作的。也就是说,每个字形必须单独绘制才能难以处理。你可以阅读更多关于它的信息here,这里有一个快速演示来帮助你。

    scatter = figure(plot_width=500, plot_height=500)
    scatter.circle(x=[1, 2, 3], y=[1, 2, 3], color='red', legend='0', alpha=0.5)
    scatter.circle(x=[4, 5], y=[4, 5], color='green', legend='1', alpha=0.5)
    scatter.legend.location = "top_right"
    scatter.legend.click_policy = "hide"
    

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 2021-03-25
      • 2015-05-02
      • 1970-01-01
      • 2015-02-04
      • 2018-06-23
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多