8000 DOC: Update broken_barh example by timhoffm · Pull Request #29488 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

DOC: Update broken_barh example #29488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions galleries/examples/lines_bars_and_markers/broken_barh.py
8000
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
"""
===========
Broken Barh
===========
======================
Broken horizontal bars
======================

Make a "broken" horizontal bar plot, i.e., one with gaps
`~.Axes.broken_barh` creates sequences of horizontal bars. This example shows
a timing diagram.
"""
import matplotlib.pyplot as plt
import numpy as np

# data is a sequence of (start, duration) tuples
cpu_1 = [(0, 3), (3.5, 1), (5, 5)]
cpu_2 = np.column_stack([np.linspace(0, 9, 10), np.full(10, 0.5)])
cpu_3 = np.column_stack([10*np.random.random(61), np.full(61, 0.05)])
cpu_4 = [(2, 1.7), (7, 1.2)]
disk = [(1, 1.5)]
network = np.column_stack([10*np.random.random(10), np.full(10, 0.05)])

# Horizontal bar plot with gaps
fig, ax = plt.subplots()
ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='tab:blue')
ax.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9),
facecolors=('tab:orange', 'tab:green', 'tab:red'))
ax.set_ylim(5, 35)
ax.set_xlim(0, 200)
ax.set_xlabel('seconds since start')
ax.set_yticks([15, 25], labels=['Bill', 'Jim']) # Modify y-axis tick labels
ax.grid(True) # Make grid lines visible
ax.annotate('race interrupted', (61, 25),
xytext=(0.8, 0.9), textcoords='axes fraction',
arrowprops=dict(facecolor='black', shrink=0.05),
fontsize=16,
horizontalalignment='right', verticalalignment='top')
# broken_barh(xranges, (ymin, height))
ax.broken_barh(cpu_1, (5.8, 0.4))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ax.broken_barh(cpu_1, (5.8, 0.4))
# xranges, yrange
ax.broken_barh(cpu_1, (5.8, 0.4))

yes it's shown in the image, but it's an easy reference point here

Copy link
Member Author
@timhoffm timhoffm Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. After thinking about it, I've decided against adding such information for the following reaons:

In the suggested form, I feel it's not obvious that xranges, yrange map to cpu_1, (5.8, 0.4)) in the line below. This is because (1) it's not clear that the comment refers to the the args of the function and (2) the structural mapping is difficult, because yrange is spelled out in a tuple and is explicit numeric values, whereas xranges is given as a named variable.

Discarded alternatives:

  • # broken_barh(xranges, yrange): Even then, the meaning of the yrange tuple is a bit mysterious and readers would have to look up the definition. (On a side node, I'd have expected either (ymin, ymax) or (ycenter, height), but not (ymin, height)).
  • # broken_barh(xranges, (ymin, height)): That would be okish, OTOH it feels a bit stupid if we have to repeat the signature as a comment.
  • a kwarg broken_barh(cpu_1, yrange=(5.8, 0.4)): While this is instructive, I don't think people would typically use this in real applications.
  • a dedicated discussion of the API in the introduction: This feels a bit heavy.

I suppose, I'm stuggling with the API. The relevant (semantic) data are the xranges. The yrange is more or less a visual detail and should not deserve so much attention. IMHO this is a bit of a design flaw, but not solvable in the example. Therefore, I've resorted to writing the example like I would as real-world code, and leave it up to the user to look up the exact API.

Copy link
Member
@story645 story645 Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# broken_barh(xranges, (ymin, height))

I understand why you feel like it's a hack but I think signature as comment is appropriate here b/c I'm hoping it reduces the cognitive load of parsing the example by explaining what those second (feel kinda arbitrary) numbers are.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Not really convinced, but it's not worth fighting over.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

ax.broken_barh(cpu_2, (4.8, 0.4))
ax.broken_barh(cpu_3, (3.8, 0.4))
ax.broken_barh(cpu_4, (2.8, 0.4))
ax.broken_barh(disk, (1.8, 0.4), color="tab:orange")
ax.broken_barh(network, (0.8, 0.4), color="tab:green")
ax.set_xlim(0, 10)
ax.set_yticks([6, 5, 4, 3, 2, 1],
labels=["CPU 1", "CPU 2", "CPU 3", "CPU 4", "disk", "network"])
ax.set_title("Resource usage")

plt.show()

Expand Down
Loading
0