hvPlot.bar#
- hvPlot.bar(x=None, y=None, stacked=False, **kwds)[source]#
A vertical bar plot
A bar plot represents categorical data with rectangular bars with heights proportional to the values that they represent. The x-axis represents the categories and the y axis represents the value scale. The bars are of equal width which allows for instant comparison of data.
Reference: https://hvplot.holoviz.org/ref/api/manual/hvplot.hvPlot.bar.html
Plotting options: https://hvplot.holoviz.org/ref/plotting_options/index.html
- Parameters:
- xstring, optional
Field name to draw x-positions from. If not specified, the index is used.
- ystring, optional
Field name to draw y-positions from. If not specified, all numerical fields are used.
- stackedbool, optional
If True, creates a stacked bar plot. Default is False.
- colorstr or array-like, optional.
The color for each of the series. Possible values are:
The name of the field to draw the colors from. The field can contain numerical values or strings representing colors.
A single color string referred to by name, RGB or RGBA code, for instance ‘red’ or ‘#a98d19’.
A sequence of color strings referred to by name, RGB or RGBA code, which will be used for each series recursively. For instance [‘red’, ‘green’,’blue’].
- **kwdsoptional
Additional keywords arguments are documented in Plotting Options. Run
hvplot.help('bar')
for the full method documentation.
- Returns:
holoviews.element.Bars
/ Panel objectYou 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
Bokeh: https://docs.bokeh.org/en/latest/docs/reference/models/glyphs/vbar.html
HoloViews: https://holoviews.org/reference/elements/bokeh/Bars.html
Matplotlib: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html
Pandas: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.bar.html
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 vertical bar plot#
Bar plots are ideal for comparing quantities across different categories. Here’s how to create a basic vertical bar chart.
import hvplot.pandas # noqa
import pandas as pd
df = pd.DataFrame({
'Project' : ['hvPlot', 'HoloViews', 'Panel'],
'Downloads': [519000, 852000, 1371000],
})
df.hvplot.bar(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.bar(title='Project downloads (multi-index)', yformatter='%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.bar(title='Project downloads (stacked multi-index)', stacked=True, yformatter='%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 x-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.bar(
x='Project',
y=['Conda', 'PyPI'],
title='Project downloads (wide-form)',
group_label='Source',
rot=45,
width=500,
yformatter='%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.bar(
x='Project',
y=['Conda', 'PyPI'],
stacked=True,
title='Project downloads (wide-form stacked)',
rot=45,
width=500,
yformatter='%i',
)
Long-form data#
In long-form data, each observation is in its own row. Setting by
can be used to break down the x-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.bar(
x='Project',
y='Downloads',
by='Source',
title='Project downloads (long-lorm)',
rot=45,
width=500,
yformatter='%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.bar(
x='Project',
y='Downloads',
by='Source',
stacked=True,
title='Project downloads (long-lorm stacked)',
rot=45,
width=500,
yformatter='%i',
)
Timeseries on the x-axis#
The x-axis can be a timeseries.
import hvplot.pandas # noqa
df = hvplot.sampledata.stocks("pandas").set_index("date").resample("1ME").mean() - 1
df.hvplot.bar(y="Netflix")
Xarray example#
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.bar(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.bar(
x='Project',
y='Downloads',
by='Source',
color=['forestgreen', 'orange'],
title='Custom bar colors',
stacked=True,
legend='top_left',
width=500,
)