{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Broken horizontal bars\n\n`~.Axes.broken_barh` creates sequences of horizontal bars. This example shows\na timing diagram.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nimport numpy as np\n\n# data is a sequence of (start, duration) tuples\ncpu_1 = [(0, 3), (3.5, 1), (5, 5)]\ncpu_2 = np.column_stack([np.linspace(0, 9, 10), np.full(10, 0.5)])\ncpu_3 = np.column_stack([10*np.random.random(61), np.full(61, 0.05)])\ncpu_4 = [(2, 1.7), (7, 1.2)]\ndisk = [(1, 1.5)]\nnetwork = np.column_stack([10*np.random.random(10), np.full(10, 0.05)])\n\nfig, ax = plt.subplots()\n# broken_barh(xranges, (ymin, height))\nax.broken_barh(cpu_1, (-0.2, 0.4))\nax.broken_barh(cpu_2, (0.8, 0.4))\nax.broken_barh(cpu_3, (1.8, 0.4))\nax.broken_barh(cpu_4, (2.8, 0.4))\nax.broken_barh(disk, (3.8, 0.4), color=\"tab:orange\")\nax.broken_barh(network, (4.8, 0.4), color=\"tab:green\")\nax.set_xlim(0, 10)\nax.set_yticks(range(6),\n labels=[\"CPU 1\", \"CPU 2\", \"CPU 3\", \"CPU 4\", \"disk\", \"network\"])\nax.invert_yaxis()\nax.set_title(\"Resource usage\")\n\nplt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.broken_barh` / `matplotlib.pyplot.broken_barh`\n - `matplotlib.axes.Axes.invert_yaxis`\n - `matplotlib.axes.Axes.set_yticks`\n\n.. tags::\n\n component: annotation\n plot-type: bar\n level: beginner\n\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}