|
1 | 1 | """
|
2 |
| -=========== |
3 |
| -Broken Barh |
4 |
| -=========== |
| 2 | +====================== |
| 3 | +Broken horizontal bars |
| 4 | +====================== |
5 | 5 |
|
6 |
| -Make a "broken" horizontal bar plot, i.e., one with gaps |
| 6 | +`~.Axes.broken_barh` creates sequences of horizontal bars. This example shows |
| 7 | +a timing diagram. |
7 | 8 | """
|
8 | 9 | import matplotlib.pyplot as plt
|
| 10 | +import numpy as np |
| 11 | + |
| 12 | +# data is a sequence of (start, duration) tuples |
| 13 | +cpu_1 = [(0, 3), (3.5, 1), (5, 5)] |
| 14 | +cpu_2 = np.column_stack([np.linspace(0, 9, 10), np.full(10, 0.5)]) |
| 15 | +cpu_3 = np.column_stack([10*np.random.random(61), np.full(61, 0.05)]) |
| 16 | +cpu_4 = [(2, 1.7), (7, 1.2)] |
| 17 | +disk = [(1, 1.5)] |
| 18 | +network = np.column_stack([10*np.random.random(10), np.full(10, 0.05)]) |
9 | 19 |
|
10 |
| -# Horizontal bar plot with gaps |
11 | 20 | fig, ax = plt.subplots()
|
12 |
| -ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='tab:blue') |
13 |
| -ax.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9), |
14 |
| - facecolors=('tab:orange', 'tab:green', 'tab:red')) |
15 |
| -ax.set_ylim(5, 35) |
16 |
| -ax.set_xlim(0, 200) |
17 |
| -ax.set_xlabel('seconds since start') |
18 |
| -ax.set_yticks([15, 25], labels=['Bill', 'Jim']) # Modify y-axis tick labels |
19 |
| -ax.grid(True) # Make grid lines visible |
20 |
| -ax.annotate('race interrupted', (61, 25), |
21 |
| - xytext=(0.8, 0.9), textcoords='axes fraction', |
22 |
| - arrowprops=dict(facecolor='black', shrink=0.05), |
23 |
| - fontsize=16, |
24 |
| - horizontalalignment='right', verticalalignment='top') |
| 21 | +# broken_barh(xranges, (ymin, height)) |
| 22 | +ax.broken_barh(cpu_1, (5.8, 0.4)) |
| 23 | +ax.broken_barh(cpu_2, (4.8, 0.4)) |
| 24 | +ax.broken_barh(cpu_3, (3.8, 0.4)) |
| 25 | +ax.broken_barh(cpu_4, (2.8, 0.4)) |
| 26 | +ax.broken_barh(disk, (1.8, 0.4), color="tab:orange") |
| 27 | +ax.broken_barh(network, (0.8, 0.4), color="tab:green") |
| 28 | +ax.set_xlim(0, 10) |
| 29 | +ax.set_yticks([6, 5, 4, 3, 2, 1], |
| 30 | + labels=["CPU 1", "CPU 2", "CPU 3", "CPU 4", "disk", "network"]) |
| 31 | +ax.set_title("Resource usage") |
25 | 32 |
|
26 | 33 | plt.show()
|
27 | 34 |
|
|
0 commit comments