Adding value labels to hvplot.bar

梦想的初衷 提交于 2020-01-25 00:19:10

问题


Using the hvplot tutorial we are trying to generate a bar plot with values as labels on the bars itself.
The bar plot is generated using the following code

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()

How can we add the value of the bars on the bar itself?


回答1:


Use holoviews hv.Labels() to add value labels to your data.

You create the labels separately and use the * symbol to overlay your labels on your plot.

Here's a working example:

# 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


Final result:



来源:https://stackoverflow.com/questions/58900736/adding-value-labels-to-hvplot-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!