将值标签添加到hvplot.bar

时间:2019-11-17 13:05:13

标签: python holoviews hvplot

我们尝试使用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()

enter image description here

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

1 个答案:

答案 0 :(得分:0)

使用全息视图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


最终结果:
enter image description here