Skip to content

Commit a2b7c78

Browse files
committed
Add examples to bar_label_demo.py
1 parent aff4ae9 commit a2b7c78

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

examples/lines_bars_and_markers/bar_label_demo.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@
9494

9595
plt.show()
9696

97+
###############################################################################
98+
# Bar labels using {}-style format string
99+
100+
fruit_names = ['Coffee', 'Salted Caramel', 'Pistachio']
101+
fruit_counts = [4000, 2000, 7000]
102+
103+
fig, ax = plt.subplots()
104+
bar_container = ax.bar(fruit_names, fruit_counts)
105+
ax.set(ylabel='pints sold', title='Gelato sales by flavor', ylim=(0, 8000))
106+
ax.bar_label(bar_container, fmt='{:,.0f}')
107+
108+
###############################################################################
109+
# Bar labels using a callable
110+
111+
animal_names = ['Lion', 'Gazelle', 'Cheetah']
112+
mph_speed = [50, 60, 75]
113+
114+
fig, ax = plt.subplots()
115+
bar_container = ax.bar(animal_names, mph_speed)
116+
ax.set(ylabel='speed in MPH', title='Running speeds', ylim=(0, 80))
117+
ax.bar_label(
118+
bar_container, fmt=lambda x: '{:.1f} km/h'.format(x * 1.61)
119+
)
120+
97121
#############################################################################
98122
#
99123
# .. admonition:: References

0 commit comments

Comments
 (0)