Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DOC: use OO-ish interface in image, contour, field examples #10865

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
Mar 27, 2018

Conversation

phobson
Copy link
Member

@phobson phobson commented Mar 22, 2018

PR Summary

Basic clean up of pyplot ambiguity in some rather complex examples.

PR Checklist

  • Code is PEP 8 compliant
  • Documentation is sphinx and numpydoc compliant

@phobson phobson force-pushed the ooify-more-examples branch from 9b63ed5 to 5c61b95 Compare March 23, 2018 15:17
@phobson phobson changed the title (WIP) DOC: use OO-ish interface in image, contour, field examples DOC: use OO-ish interface in image, contour, field examples Mar 23, 2018
Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

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...

@@ -34,13 +34,11 @@
norm = cm.colors.Normalize(vmax=abs(Z).max(), vmin=-abs(Z).max())
cmap = cm.PRGn

fig = plt.figure()
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm with you on that. We do it both ways. And while I think consistency is good, maybe showing options are important too

Copy link
Contributor

Choose a reason for hiding this comment

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

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)
Copy link
Member

Choose a reason for hiding this comment

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

Why not reuse CS?

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

@@ -34,6 +34,6 @@

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))
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need the str here. set_title does that as well...

Copy link
Member Author

Choose a reason for hiding this comment

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

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"

Copy link
Member

Choose a reason for hiding this comment

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

Oh, yes, I guess None might have a different meaning. Fair enough

@@ -29,33 +29,31 @@
interp_cubic_min_E = mtri.CubicTriInterpolator(triang, z, kind='min_E')
zi_cubic_min_E = interp_cubic_min_E(xi, yi)

# Set up the figure
fig, axes = plt.subplots(nrows=2, ncols=2)
Copy link
Member

Choose a reason for hiding this comment

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

Not a fan of axes becuase of the plural/singular ambiguity....

Copy link
Member Author

Choose a reason for hiding this comment

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

fair point

Copy link
Member

Choose a reason for hiding this comment

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

super minor - I'd only change if someone else has serious issues...

@@ -22,24 +22,21 @@
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)
Copy link
Member

Choose a reason for hiding this comment

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

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)
Copy link
Member

Choose a reason for hiding this comment

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

line is not used afterwards, so it should be left out. Same with the return values on the following line.

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well said. I'm on aboard.

@phobson phobson force-pushed the ooify-more-examples branch from fb2ea82 to ec5e886 Compare March 26, 2018 22:50
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:
Copy link
Member

Choose a reason for hiding this comment

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

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.

@phobson
Copy link
Member Author

phobson commented Mar 26, 2018 via email

@jklymak jklymak merged commit ad3a303 into matplotlib:master Mar 27, 2018
@phobson phobson deleted the ooify-more-examples branch April 30, 2018 21:25
@tacaswell tacaswell added this to the v3.0 milestone Jun 28, 2018
tacaswell pushed a commit to tacaswell/matplotlib that referenced this pull request Jun 28, 2018
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
tacaswell pushed a commit to tacaswell/matplotlib that referenced this pull request Jun 28, 2018
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants