Use sublots in examples #1598

Closed
wants to merge 5 commits into
from

Projects

None yet

5 participants

@ivanov
Member
ivanov commented Dec 14, 2012

this is a rebase of #1462 against master, as discussed on that PR. I believe it is ready to merge

ivanov added some commits Nov 8, 2012
@ivanov ivanov use plt.subplots() in examples as much as possible
At the recent LBL Software Carpentry Workshop, it was pointed out that there's
an inconsistency within our documentation for how to create new figures with
subplots.

Indeed, most examples were using the old way, something like:

    fig = plt.figure()
    ax = plt.subplot(111) # or plt.add_subplot(111)

This patch changes a whole bunch of instances like the above to:

    fig, ax = plt.subplots()

We should strive to have a minimal amount of constants in our code,
especially unusual ones like `111`, which only make sense to Matlab
refugees.

I have left unchanged examples which were using axes keywords passed to
subplot() and add_subplot(), since those end up transforming things like:

    figure()
    subplot(111, axisbg='w')

to

    plt.subplots(subplot_kw=dict(axisbg='w'))

which isn't necessarily better.

I also did not touch most of the user_interfaces examples, since those did not
involve using plt, but instead explicitly imported Figure, and used the OO
approach on Figure instances.

Also updated instaces where the old "import pylab as p" convention was used to
use our standard "import matplotlib.pyplot as plt"

I have also updated some, but not all uses of subplot(121) etc, but I'm a bit
exhausted after doing all of these.
1b9ade2
@ivanov ivanov revert to OO interface (no pyplot) in OO example d42109a
@ivanov ivanov more `subplots`, cleanup of old `import *` style a623ff3
@ivanov ivanov whats_new: a note about the changed examples bf8d1bc
@mdboom mdboom commented on an outdated diff Dec 17, 2012
doc/users/whats_new.rst
@@ -33,6 +33,14 @@ Andrew Dawson added a new keyword argument *extendrect* to
:meth:`~matplotlib.pyplot.colorbar` to optionally make colorbar
extensions rectangular instead of triangular.
+Examples now use subplots()
+---------------------------
+For the sake of brevity and clarity, most of the :ref:`examples
+<examples-index>` now use the newer :func:`~matplotlib.pyplot.subplots`
+, which creates a figure and one (or multiple) axes object(s) in one
mdboom
mdboom Dec 17, 2012 Owner

comma shouldn't be line wrapped here

Owner
mdboom commented Dec 17, 2012

I haven't picked through this with a fine-toothed comb, but +1 in general.

We should try to get this merged before the MEP12 work gets underway -- this is a good start to the example/gallery cleanup that is planned.

@dmcdougall dmcdougall and 1 other commented on an outdated diff Dec 17, 2012
examples/animation/old_animation/animation_blit_qt.py
@@ -24,13 +24,13 @@ class BlitQT(QObject):
def __init__(self):
QObject.__init__(self, None, "app")
- self.ax = p.subplot(111)
+ self.ax = plt.axes()
dmcdougall
dmcdougall Dec 17, 2012 Member

fig isn't used anywhere? Is that why you didn't do the usual fig, ax = plt.subplots() here?

ivanov
ivanov Dec 18, 2012 Member

yes, that's right, using axes was suggested by @pelson here. Though now that this is back against master, I will just change this to the new plt.subplot()

@dmcdougall dmcdougall commented on the diff Dec 17, 2012
examples/animation/old_animation/animation_blit_tk.py
import numpy as npy
import time
-ax = p.subplot(111)
-canvas = ax.figure.canvas
+fig, ax = plt.subplots()
+canvas = fig.canvas
@dmcdougall dmcdougall and 1 other commented on an outdated diff Dec 17, 2012
examples/animation/old_animation/animation_blit_wx.py
@@ -24,7 +25,7 @@
matplotlib.backends.backend_wxagg._use_accelerator(False)
-ax = p.subplot(111)
+fig, ax = plt.subplots()
canvas = ax.figure.canvas
dmcdougall
dmcdougall Dec 17, 2012 Member

Maybe canvas = fig.canvas instead?

ivanov
ivanov Dec 18, 2012 Member

agreed.

@dmcdougall dmcdougall commented on the diff Dec 17, 2012
examples/animation/old_animation/draggable_legend.py
ax.plot([1,2,3], label="test")
l = ax.legend()
d1 = l.draggable()
-xy = 1, 2
+xy = 1, 2
@dmcdougall dmcdougall commented on the diff Dec 17, 2012
examples/api/collections_demo.py
@@ -41,69 +41,63 @@
# Make a list of colors cycling through the rgbcmyk series.
colors = [colorConverter.to_rgba(c) for c in ('r','g','b','c','y','m','k')]
-fig = plt.figure()
+fig, axes = plt.subplots(2,2)
+((ax1, ax2), (ax3, ax4)) = axes # unpack the axes
dmcdougall
dmcdougall Dec 17, 2012 Member

Wouldn't it be better to use axes[0,0], axes[1,0], ..., rather than using all these?

ivanov
ivanov Dec 18, 2012 Member

I tried it that way, but since each axes instance is utilized many times over, it ends up looking cumbersome

@dmcdougall dmcdougall and 1 other commented on an outdated diff Dec 17, 2012
examples/axes_grid/inset_locator_demo.py
@@ -14,10 +14,9 @@ def add_sizebar(ax, size):
ax.add_artist(asb)
-fig = plt.figure(1, [5.5, 3])
+fig, (ax,ax2) = plt.subplots(1,2, figsize=[5.5, 3])
dmcdougall
dmcdougall Dec 17, 2012 Member

Sorry to be picky, but could you add a space here? So that it reads fig, (ax, ax2)?

ivanov
ivanov Dec 18, 2012 Member

sounds good, adding one between the subplots arguments, as well

@dmcdougall dmcdougall commented on the diff Dec 17, 2012
examples/axes_grid/scatter_hist.py
@@ -1,17 +1,15 @@
import numpy as np
import matplotlib.pyplot as plt
+from mpl_toolkits.axes_grid1 import make_axes_locatable
dmcdougall
dmcdougall Dec 17, 2012 Member

Why was this added? There are no references to make_axes_locatable added.

@dmcdougall dmcdougall commented on the diff Dec 17, 2012
examples/axes_grid/scatter_hist.py
# the random data
x = np.random.randn(1000)
y = np.random.randn(1000)
-fig = plt.figure(1, figsize=(5.5,5.5))
-
-from mpl_toolkits.axes_grid1 import make_axes_locatable
dmcdougall
dmcdougall Dec 17, 2012 Member

Oh, my bad. It was just moved up. Ignore my previous comment.

@dmcdougall dmcdougall commented on the diff Dec 17, 2012
examples/pylab_examples/custom_ticker1.py
ax.yaxis.set_major_formatter(formatter)
-bar(x, money)
-xticks( x + 0.5, ('Bill', 'Fred', 'Mary', 'Sue') )
-show()
+plt.bar(x, money)
dmcdougall
dmcdougall Dec 17, 2012 Member

plt.bar or ax.bar? It probably doesn't matter...

ivanov
ivanov Dec 17, 2012 Member

since it's in the pylab examples, I thought to leave it with the plt interface.

dmcdougall
dmcdougall Dec 18, 2012 Member

Ok, cool.

Member

I fine-toothed it. That took a fair bit of stamina, but I think I've provided some helpful feedback for @ivanov. Let me know if there's anything else I can do. Other than that I think this is good to go.

@mdboom Was there a consensus on whether this should also go into v1.2.x?

Member
ivanov commented Dec 17, 2012

@dmcdougall I think the final word was that this should not go into v1.2.x, which is why I closed that PR. Thanks for your feedback, I'll just add a commit addressing this round of reviews shortly

Member

Surely there is no way b34b6b7 caused that Python 3.2 Travis failure...

Member
ivanov commented Dec 18, 2012

yeah, the false positive rate for Travis is pretty frustrating :\

Owner
mdboom commented Jan 16, 2013

Sorry this got dropped. I'm worried that it may be really difficult to merge with #1623 which moves around many of the examples. Maybe this could be rebased off of #1623 and merged in there. In any event, this will need some coordination between at least @ivanov, @tonysyu and myself.

Member
pelson commented Apr 18, 2013

Ouch. I agree with @mdboom on this one - this is very much desirable, but getting this into a position where it plays nicely with @tonysyu changes (and any other changes which have happened since this PR was created) could be a nightmare. I think smaller PRs are the way forward here.

Owner
mdboom commented May 21, 2013

Sometimes git really shines. It actually was able to do this rebase fairly smoothly and tracked the many moved files just fine. I'm put up a rebased version of this PR in #2043 so @ivanov's hard work doesn't go to waste. I can't guarantee it's complete (i.e. using subplots everywhere), but it's a definite improvement.

Owner
efiring commented May 25, 2013

I'm closing this because it is replaced by #2043.

@efiring efiring closed this May 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment