【问题标题】:Adding value labels to hvplot.bar向 hvplot.bar 添加值标签
【发布时间】:2019-11-18 04:37:35
【问题描述】:

使用hvplot tutorial,我们正在尝试生成一个条形图,其中值作为条形本身的标签。
使用以下代码生成条形图

import dask.dataframe as dd
import hvplot.pandas
df = dd.read_parquet('data/earthquakes.parq').persist()  
df.magType.value_counts().compute().to_frame().hvplot.bar()

我们如何在条形本身上添加条形的值?

【问题讨论】:

    标签: python holoviews hvplot


    【解决方案1】:

    使用 holoviews hv.Labels() 为您的数据添加值标签。

    您单独创建标签并在您的绘图上创建use the * symbol to overlay your labels

    这是一个工作示例:

    # import libraries
    import numpy as np
    import pandas as pd
    
    import holoviews as hv
    import hvplot.pandas
    
    # create some sample data
    df = pd.DataFrame({
        'col1': ['bar1', 'bar2', 'bar3'],
        'col2': np.random.rand(3)
    })
    
    # create your plot
    plot = df.hvplot(kind='bar', x='col1', y='col2', ylim=(0, 1.2))
    
    # create your labels separately
    # kdims specifies the x and y position of your value label
    # vdims specifies the column to use for the value text of your label
    labels = hv.Labels(data=df, kdims=['col1', 'col2'], vdims='col2')
    
    # use the * symbol to overlay your labels on your plot
    plot * labels
    


    最终结果:

    【讨论】:

    • 我们尝试使用yoffset,如Labels guide 所示,但随后图表消失了——我们在做什么? `labels = hv.Labels(data=df, kdims=['col1', 'col2'], vdims='col2').opts(hv.opts.Labels(yoffset=0.05))`
    • 我遇到了同样的问题,也许您可​​以在这里询问您的问题:gitter.im/pyviz/pyviz 这些是开发人员,他们对问题非常敏感,也许他们知道解决方案。
    • 如果您使用 hv.extension('plotly') 将 holoviews 后端切换为 plotly,则 yoffset 正在工作。因此,在全息视图中,它适用于情节,但不适用于散景后端。
    • 感谢更新,不幸的是我的环境中没有情节
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多