Labeling horizontal bar using matplotlib bar_label
Question:
I’d like to display the value of the bar on each bar with pyplot.barh()
and ax.bar_label
.
I’ve read the document https://matplotlib.org/stable/gallery/lines_bars_and_markers/bar_label_demo.html but it didn’t help. Please help.
# Prepare & sort the data
df = pd.DataFrame({"coins": ["BTC", "ETH", "BNB", "XRP", "SOL", "ADA"],
"dominance": [42.08, 19.38, 3.68, 1.73, 1.55, 1.51]})
df = df.sort_values(by=["dominance"])
# Plot horizontal bar graph
plt.figure(figsize=[10, 9])
plt.barh(df.coins, df.dominance)
Answers:
the issue is that the bar_label
is a new feature and available in matplotlib version 3.4 or later – ref this link. Check the version of matplotlib using
import matplotlib
print('matplotlib: {}'.format(matplotlib.__version__))
Upgrade to v3.4
or latest version using pip install matplotlib --upgrade
. You will need to include the code in the link you provided and as mentioned by JohanC and be able to see bar labels
I’d like to display the value of the bar on each bar with pyplot.barh()
and ax.bar_label
.
I’ve read the document https://matplotlib.org/stable/gallery/lines_bars_and_markers/bar_label_demo.html but it didn’t help. Please help.
# Prepare & sort the data
df = pd.DataFrame({"coins": ["BTC", "ETH", "BNB", "XRP", "SOL", "ADA"],
"dominance": [42.08, 19.38, 3.68, 1.73, 1.55, 1.51]})
df = df.sort_values(by=["dominance"])
# Plot horizontal bar graph
plt.figure(figsize=[10, 9])
plt.barh(df.coins, df.dominance)
the issue is that the bar_label
is a new feature and available in matplotlib version 3.4 or later – ref this link. Check the version of matplotlib using
import matplotlib
print('matplotlib: {}'.format(matplotlib.__version__))
Upgrade to v3.4
or latest version using pip install matplotlib --upgrade
. You will need to include the code in the link you provided and as mentioned by JohanC and be able to see bar labels