hvPlot.barh#

hvPlot.barh(x=None, y=None, stacked=False, **kwds)[source]#

A horizontal bar plot

A barh plot represents categorical data with rectangular bars with heights proportional to the values that they represent. The y-axis of the chart plots categories and the x-axis represents the value scale. The bars are of equal width which allows for instant comparison of data.

barh can be used on dataframes with regular Index or MultiIndex.

Reference: https://hvplot.holoviz.org/ref/api/manual/hvplot.hvPlot.barh.html

Plotting options: https://hvplot.holoviz.org/ref/plotting_options/index.html

Parameters:
stackedbool, optional

If True, creates a stacked horizontal bar plot. Default is False.

**kwdsoptional

Additional keywords arguments are documented in Plotting Options. Run hvplot.help('barh') for the full method documentation.

Returns:
holoviews.element.Bars / Panel object

You can print the object to study its composition and run:

import holoviews as hv
hv.help(the_holoviews_object)

to learn more about its parameters and options.

References

Backend-specific styling options#

alpha, bar_width, cmap, color, fill_alpha, fill_color, hover_alpha, hover_color, hover_fill_alpha, hover_fill_color, hover_line_alpha, hover_line_cap, hover_line_color, hover_line_dash, hover_line_dash_offset, hover_line_join, hover_line_width, line_alpha, line_cap, line_color, line_dash, line_dash_offset, line_join, line_width, muted, muted_alpha, muted_color, muted_fill_alpha, muted_fill_color, muted_line_alpha, muted_line_cap, muted_line_color, muted_line_dash, muted_line_dash_offset, muted_line_join, muted_line_width, nonselection_alpha, nonselection_color, nonselection_fill_alpha, nonselection_fill_color, nonselection_line_alpha, nonselection_line_cap, nonselection_line_color, nonselection_line_dash, nonselection_line_dash_offset, nonselection_line_join, nonselection_line_width, selection_alpha, selection_color, selection_fill_alpha, selection_fill_color, selection_line_alpha, selection_line_cap, selection_line_color, selection_line_dash, selection_line_dash_offset, selection_line_join, selection_line_width, visible

align, alpha, c, capsize, color, ec, ecolor, edgecolor, error_kw, facecolor, fc, hatch, log, visible

Examples#

Simple horizontal bar plot#

Bar plots are ideal for comparing quantities across different categories. Here’s how to create a basic horizontal bar chart./

import hvplot.pandas  # noqa
import pandas as pd

df = pd.DataFrame({
    'Project' : ['hvPlot', 'HoloViews', 'Panel'],
    'Downloads': [519000, 852000, 1371000],
})

df.hvplot.barh(x='Project', y='Downloads', title='Project downloads', width=500)

Multi-index bar plot#

When working with multi-index data, hvPlot can automatically interpret the hierarchical index to create nested categories on the x-axis, the outer index level being displayed as the outer category.

import hvplot.pandas  # noqa
import pandas as pd

multi_index_df = pd.DataFrame({
    'Project' : ['hvPlot', 'hvPlot', 'HoloViews', 'HoloViews', 'Panel', 'Panel'],
    'Source' : ['Conda', 'PyPI', 'Conda', 'PyPI', 'Conda', 'PyPI'],
    'Downloads': [112000, 407000, 132000, 720000, 171000, 1200000],
}).set_index(['Project', 'Source'])
print(multi_index_df.head(3))

multi_index_df.hvplot.barh(title='Project downloads (multi-index)', xformatter='%i')
                  Downloads
Project   Source           
hvPlot    Conda      112000
          PyPI       407000
HoloViews Conda      132000

You can instead stack on the y-axis the values of the nested index ('project' in this example), by setting stacked to True.

import hvplot.pandas  # noqa
import pandas as pd

multi_index_df = pd.DataFrame({
    'Project' : ['hvPlot', 'hvPlot', 'HoloViews', 'HoloViews', 'Panel', 'Panel'],
    'Source' : ['Conda', 'PyPI', 'Conda', 'PyPI', 'Conda', 'PyPI'],
    'Downloads': [112000, 407000, 132000, 720000, 171000, 1200000],
}).set_index(['Project', 'Source'])
print(multi_index_df.head(3))

multi_index_df.hvplot.barh(
    title='Project downloads (stacked multi-index)',
    stacked=True,
    width=500,
    legend='bottom_right',
    xformatter='%i', 
)
                  Downloads
Project   Source           
hvPlot    Conda      112000
          PyPI       407000
HoloViews Conda      132000

Wide-form data#

In wide-form data, each variable is in its own column. To plot multiple categories on the y-axis when the data is in this form, you need to provide a list of columns to y.

import hvplot.pandas  # noqa
import pandas as pd

wide_df = pd.DataFrame({
    'Project' : ['hvPlot', 'HoloViews', 'Panel'],
    'Conda' : [112000, 132000, 171000],
    'PyPI': [407000, 720000, 1200000],
})

wide_df.hvplot.barh(
    x='Project',
    y=['Conda', 'PyPI'],
    title='Project downloads (wide-form)',
    group_label='Source',
    width=500,
    xformatter='%i',
)

Setting stacked=True produces a stacked plot.

import hvplot.pandas  # noqa
import pandas as pd

wide_df = pd.DataFrame({
    'Project' : ['hvPlot', 'HoloViews', 'Panel'],
    'Conda' : [112000, 132000, 171000],
    'PyPI': [407000, 720000, 1200000],
})

wide_df.hvplot.barh(
    x='Project',
    y=['Conda', 'PyPI'],
    stacked=True,
    title='Project downloads (wide-form stacked)',
    width=500,
    legend='bottom_right',
    xformatter='%i',
)

Long-form data#

In long-form data, each observation is in its own row. Setting by can be used to break down the y-axis in multiple sub-categories.

import hvplot.pandas  # noqa
import pandas as pd

long_df = pd.DataFrame({
    'Project' : ['hvPlot', 'hvPlot', 'HoloViews', 'HoloViews', 'Panel', 'Panel'],
    'Source' : ['Conda', 'PyPI', 'Conda', 'PyPI', 'Conda', 'PyPI'],
    'Downloads': [112000, 407000, 132000, 720000, 171000, 1200000],
})

long_df.hvplot.barh(
    x='Project',
    y='Downloads',
    by='Source',
    title='Project downloads (long-lorm)',
    width=500,
    xformatter='%i',
)

Setting stacked=True produces a stacked plot.

import hvplot.pandas  # noqa
import pandas as pd

long_df = pd.DataFrame({
    'Project' : ['hvPlot', 'hvPlot', 'HoloViews', 'HoloViews', 'Panel', 'Panel'],
    'Source' : ['Conda', 'PyPI', 'Conda', 'PyPI', 'Conda', 'PyPI'],
    'Downloads': [112000, 407000, 132000, 720000, 171000, 1200000],
})

long_df.hvplot.barh(
    x='Project',
    y='Downloads',
    by='Source',
    stacked=True,
    title='Project downloads (long-lorm stacked)',
    width=500,
    legend='bottom_right',
    xformatter='%i',
)

Xarray examples#

import hvplot.xarray  # noqa

ds = hvplot.sampledata.air_temperature("xarray")
ds = ds.sel(lon=285.,lat=40.).groupby('time.dayofyear').mean() - 273.15

ds.hvplot.barh(y="air", title="Air temperature by day", ylabel="T [°C]", c="air")

Customizing bar colors#

You can customize the appearance of bar plots by assigning specific colors to each variable. This makes it easier to distinguish between multiple bars in stacked or grouped charts.

import hvplot.pandas  # noqa
import pandas as pd

df = pd.DataFrame({
    'Project' : ['hvPlot', 'hvPlot', 'HoloViews', 'HoloViews', 'Panel', 'Panel'],
    'Source' : ['Conda', 'PyPI', 'Conda', 'PyPI', 'Conda', 'PyPI'],
    'Downloads': [112000, 407000, 132000, 720000, 171000, 1200000],
})

df.hvplot.barh(
    x='Project',
    y='Downloads',
    by='Source',
    color=['forestgreen', 'orange'],
    title='Custom bar colors',
    stacked=True,
    legend='bottom_right',
    width=500,
    xformatter='%i',
)
This web page was generated from a Jupyter notebook and not all interactivity will work on this website.