|
10 | 10 | # data from https://allisonhorst.github.io/palmerpenguins/
|
11 | 11 |
|
12 | 12 | import matplotlib.pyplot as plt
|
13 |
| -import numpy as np |
14 | 13 |
|
15 | 14 | species = ("Adelie", "Chinstrap", "Gentoo")
|
16 | 15 | penguin_means = {
|
|
19 | 18 | 'Flipper Length': (189.95, 195.82, 217.19),
|
20 | 19 | }
|
21 | 20 |
|
22 |
| -x = np.arange(len(species)) # the label locations |
23 |
| -width = 0.25 # the width of the bars |
24 |
| -multiplier = 0 |
25 |
| - |
26 | 21 | fig, ax = plt.subplots(layout='constrained')
|
27 | 22 |
|
28 |
| -for attribute, measurement in penguin_means.items(): |
29 |
| - offset = width * multiplier |
30 |
| - rects = ax.bar(x + offset, measurement, width, label=attribute) |
31 |
| - ax.bar_label(rects, padding=3) |
32 |
| - multiplier += 1 |
| 23 | +res = ax.grouped_bar(penguin_means, tick_labels=species, group_spacing=1) |
| 24 | +for container in res.bar_containers: |
| 25 | + ax.bar_label(container, padding=3) |
33 | 26 |
|
34 |
| -# Add some text for labels, title and custom x-axis tick labels, etc. |
| 27 | +# Add some text for labels, title, etc. |
35 | 28 | ax.set_ylabel('Length (mm)')
|
36 | 29 | ax.set_title('Penguin attributes by species')
|
37 |
| -ax.set_xticks(x + width, species) |
38 | 30 | ax.legend(loc='upper left', ncols=3)
|
39 | 31 | ax.set_ylim(0, 250)
|
40 | 32 |
|
|
0 commit comments