|
94 | 94 |
|
95 | 95 | plt.show() |
96 | 96 |
|
| 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 | + |
97 | 121 | ############################################################################# |
98 | 122 | # |
99 | 123 | # .. admonition:: References |
|
0 commit comments