import pandas as pd
import hvplot.pandas # noqa
A bar
plot represents categorical data with rectangular bars with heights proportional to the numerical values that they represent.
The x-axis represents the categories and the y axis represents the numerical value scale.
The bars are of equal width which allows for instant comparison of data.
pd.DataFrame({
"framework": ["hvPlot", "HoloViews", "Panel"],
"stars": [700, 2400, 2600]
}).hvplot.bar(x="framework", y="stars", color="gold", title="Bar Plot of Github Stars", ylabel="⭐")
Let's import some data.
from bokeh.sampledata.autompg import autompg_clean as autompg
autompg.head()
We define long form data, i.e. one row per yr
categorical value.
autompg_long_form = autompg.groupby("yr").mean(numeric_only=True).reset_index()
autompg_long_form.head()
We define a dataset with a multi index representing multiple categories
autompg_multi_index = autompg.query("yr<=80").groupby(['yr', 'origin']).mean(numeric_only=True)
autompg_multi_index.head()
We define wide form data, i.e. multiple columns representing a category like origin
.
autompg_wide = autompg_multi_index.reset_index().pivot(index='yr', columns='origin', values='mpg')
autompg_wide.head()
You can plot long form data if you specify the categorical x-value using the x
argument and the numerical y-value using the y-argument
.
autompg_long_form.hvplot.bar(x="yr", y="mpg", width=1000)
If you don't specify the x
argument, then the index will be used.
autompg_long_form.hvplot.bar(y="mpg", width=1000)
When the index is a MultiIndex
, the x-axis represents the multiple categories included in the index, the outer index level being displayed as the outer category.
autompg_multi_index.hvplot.bar(width=1000, rot=90)
You can instead stack on the y-axis the values of the nested index/category, origin in this example, by setting stacked
to True
.
autompg_multi_index.hvplot.bar(stacked=True, width=1000, legend="top_left", height=500)
To plot multiple categories on the x-axis when the data is wide form, you need to provide a list of columns to y
.
autompg_wide.hvplot.bar(y=['Asia', 'Europe', 'North America'], width=1000, ylabel="mpg", rot=90)
And you may also stack the values of the wide form data.
autompg_wide.hvplot.bar(y=['Asia', 'Europe', 'North America'], ylabel="mpg", stacked=True, width=1000, legend="top_left", height=500)
You can control the bar
color using the color
argument. It accepts the name of a column, the name of a color or a list of colors.
Here is an example using a single named color.
autompg_long_form.hvplot.bar(x="yr", y="mpg", color="teal", width=1000)
Here is an example using a list of colors.
autompg_wide.hvplot.bar(y=['Asia', 'Europe', 'North America'], width=1000, ylabel="mpg", color=["#ba2649", "#ffa7ca", "#1a6b54"], rot=90)
Here is an example using the name of a column.
autompg_long_form.hvplot.bar(y='mpg', color="weight", colorbar=True, clabel="Weight", cmap="bmy", width=1000)