DOC: use OO-ish interface in image, contour, field examples#10865
Conversation
9b63ed5 to
5c61b95
Compare
jklymak
left a comment
There was a problem hiding this comment.
I think this is great. A couple of minor quibbles that are totally take-it-or-leave-it. I've not gone through and actually checked that all the examples still render OK. Presumably you've done that...
| cmap = cm.PRGn | ||
|
|
||
| fig = plt.figure() | ||
| fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2) |
There was a problem hiding this comment.
I'm not a huge fan of this syntax, versus just fig, axs = and then setting ax = axs[row, col]. But not a show-stopper at all.
There was a problem hiding this comment.
I'm with you on that. We do it both ways. And while I think consistency is good, maybe showing options are important too
There was a problem hiding this comment.
I'm not huge fan of putting arrays in variable names (ax1, ax2, ax3, ax4) rather than just the type (ax[0]). The former is so much harder to change later on, so I'm not sure we should actually be promoting it. If the row/column nature is a problem, you can always do ax = ax.flatten(). My $0.02.
|
|
||
| # Basic contour plot | ||
| CS = plt.contour(X, Y, Z) | ||
| CS1 = ax1.contour(X, Y, Z) |
There was a problem hiding this comment.
My thought (which is behind e.g., fig1, ax1 as well) is that I want it to be completely clear to a totally "n00b" (to programming, python, or matplotlib) that we're starting over and working on something new.
| for ax, interp_method in zip(axes.flat, methods): | ||
| ax.imshow(grid, interpolation=interp_method, cmap='viridis') | ||
| ax.set_title(interp_method) | ||
| ax.set_title(str(interp_method)) |
There was a problem hiding this comment.
I don't think you need the str here. set_title does that as well...
There was a problem hiding this comment.
When I looked at the rendered page, the None plot has no title, but in the current docs, is does have a title that says "None"
There was a problem hiding this comment.
Oh, yes, I guess None might have a different meaning. Fair enough
| zi_cubic_min_E = interp_cubic_min_E(xi, yi) | ||
|
|
||
| # Set up the figure | ||
| fig, axes = plt.subplots(nrows=2, ncols=2) |
There was a problem hiding this comment.
Not a fan of axes becuase of the plural/singular ambiguity....
There was a problem hiding this comment.
super minor - I'd only change if someone else has serious issues...
| data = np.array(data, dtype=[('x', np.float32), ('y', np.float32), | ||
| ('u', np.float32), ('v', np.float32)]) | ||
|
|
||
| fig, axes1 = plt.subplots(nrows=2, ncols=2) |
There was a problem hiding this comment.
Since fig, doesn't have a number, axes1 shouldn't either.
I propose to decide on one canonical way of writing the second argument: axs vs. axes. Both seem to be used. I slightly tend to axs in the sense of multiple ax. Since Axes and Axis are both class names axes may just too easily be misinterpreted as multiple Axis or a single Axes instance.
So proposed value here is axs.
| plt.subplot(212, sharex=ax1) | ||
| Pxx, freqs, bins, im = plt.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900) | ||
| fig, (ax1, ax2) = plt.subplots(nrows=2) | ||
| line, = ax1.plot(t, x) |
There was a problem hiding this comment.
line is not used afterwards, so it should be left out. Same with the return values on the following line.
There was a problem hiding this comment.
I'm going to push back on this a bit. My gut feeling is that users -- especially new users -- are more likely to read these example than the docstrings.
So I think we should use this examples to be as explicit as possible about what each method is doing.
There are a lot of questions on SO along the lines of "how do I get an artist that's on my plot". While, there's a decent way to do that after the fact, I think it'd be nice to get users in the habbit of capturing things sooner rather than searching for them later.
There was a problem hiding this comment.
I'm 100% with you that users are learning from the examples. Therefore a big 👍 for updating the examples.
My conclusion from learning by example is that the examples should be similar to real-world uses. I fear that if all the examples had line = ax.plot(x, y), you'll find a lot of people having exactly that in their scripts, even if they don't use line at all - at least that's my experience with less-experienced users.
I'm fine, if you want to keep the return values on specgram(). That's what the example is about. If so, it would be good to move the related comment from a few lines above directly to the function call. Something like:
Pxx, freqs, bins, im = ax2.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
# returned values:
# - Pxx: the periodograms
# - freqs: the frequency vector
# - bins: the centers of the time bins
# - im: the matplotlib.image.AxesImage instance representing the data in the plot
However, since this is not about plot(), I wouldn't add the unused line.
There was a problem hiding this comment.
Well said. I'm on aboard.
fb2ea82 to
ec5e886
Compare
| fig, (ax1, ax2) = plt.subplots(nrows=2) | ||
| ax1.plot(t, x) | ||
| Pxx, freqs, bins, im = ax2.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900) | ||
| # The `specgram` method returns 4 objects. They are: |
There was a problem hiding this comment.
Unfortunately sphinx references don't work in code comments. See the resulting file.
I'll leave it up to you, if you want to keep it anyway.
|
Oh yeah. I wasn't trying to sphinx format. that was just habit.
…On Mon, Mar 26, 2018 at 4:45 PM, Tim Hoffmann ***@***.***> wrote:
***@***.**** approved this pull request.
------------------------------
In examples/images_contours_and_fields/specgram_demo.py
<#10865 (comment)>
:
> Fs = int(1.0 / dt) # the sampling frequency
-# Pxx is the segments x freqs array of instantaneous power, freqs is
-# the frequency vector, bins are the centers of the time bins in which
-# the power is computed, and im is the matplotlib.image.AxesImage
-# instance
-
-ax1 = plt.subplot(211)
-plt.plot(t, x)
-plt.subplot(212, sharex=ax1)
-Pxx, freqs, bins, im = plt.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
+fig, (ax1, ax2) = plt.subplots(nrows=2)
+ax1.plot(t, x)
+Pxx, freqs, bins, im = ax2.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
+# The `specgram` method returns 4 objects. They are:
Unfortunately sphinx references don't work in code comments. See the resulting
file
<https://8320-1385122-gh.circle-artifacts.com/0/home/circleci/project/doc/build/html/gallery/images_contours_and_fields/specgram_demo.html#sphx-glr-gallery-images-contours-and-fields-specgram-demo-py>
.
I'll leave it up to you, if you want to keep it anyway.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#10865 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABHCoxDqM-kOFA2t9Fo6qxdcuguUK0SIks5tiX2fgaJpZM4S3s7h>
.
|
DOC: use OO-ish interface in image, contour, field examples This removes change to lib/matplotlib/axes/_base.py in PR matplotlib#10865 and commit ad3a303
DOC: use OO-ish interface in image, contour, field examples This removes change to lib/matplotlib/axes/_base.py in PR matplotlib#10865 and commit ad3a303
PR Summary
Basic clean up of
pyplotambiguity in some rather complex examples.PR Checklist