Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
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.
  • Loading branch information
ivanov committed Nov 8, 2012
commit 5c54d8f3acac8c866ab0b75c17b7a8fb877db74d
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2012-11-08 Replaced plt.figure and plt.subplot calls by the newer, more
convenient single call to plt.subplots() in the documentation
examples - PI

2012-08-11 Fix path-closing bug in patches.Polygon, so that regardless
of whether the path is the initial one or was subsequently
set by set_xy(), get_xy() will return a closed path if and
Expand Down
3 changes: 1 addition & 2 deletions examples/animation/animate_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def data_gen():
yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
data_gen.t = 0

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.set_ylim(-1.1, 1.1)
ax.set_xlim(0, 5)
Expand Down
3 changes: 1 addition & 2 deletions examples/animation/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import matplotlib.path as path
import matplotlib.animation as animation

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()

# histogram our data with numpy
data = np.random.randn(1000)
Expand Down
3 changes: 1 addition & 2 deletions examples/animation/old_animation/animate_decay_tk_blit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ def data_gen():
return np.sin(2*np.pi*t) * np.exp(-t/10.)
data_gen.t = 0

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
line, = ax.plot([], [], animated=True, lw=2)
ax.set_ylim(-1.1, 1.1)
ax.set_xlim(0, 5)
Expand Down
12 changes: 6 additions & 6 deletions examples/animation/old_animation/animation_blit_fltk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import fltk
import matplotlib
matplotlib.use('FltkAgg')
import pylab as p
import matplotlib.pyplot as plt
import numpy as npy
import time

Expand Down Expand Up @@ -42,13 +42,13 @@ def update(self,ptr):
sys.exit()
return True

ax = p.subplot(111)
p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
p.grid() # to ensure proper background restore
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
plt.grid() # to ensure proper background restore
# create the initial line
x = npy.arange(0,2*npy.pi,0.01)
line, = p.plot(x, npy.sin(x), animated=True)
p.draw()
line, = plt.plot(x, npy.sin(x), animated=True)
plt.draw()
anim=animator(ax)

fltk.Fl.add_idle(anim.update)
Expand Down
3 changes: 1 addition & 2 deletions examples/animation/old_animation/animation_blit_gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import matplotlib.pyplot as plt


fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
canvas = fig.canvas

fig.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
Expand Down
3 changes: 1 addition & 2 deletions examples/animation/old_animation/animation_blit_gtk2.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,8 @@ def update_line(self, *args):


plt.rcParams["text.usetex"] = False
fig = plt.figure()

ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.xaxis.set_animated(True)
ax.yaxis.set_animated(True)
canvas = fig.canvas
Expand Down
2 changes: 1 addition & 1 deletion examples/animation/old_animation/animation_blit_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BlitQT(QObject):
def __init__(self):
QObject.__init__(self, None, "app")

self.ax = p.subplot(111)
fig, self.ax = plt.subplots()
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 sure whether:

fig, ax = plt.subplots()

is better than

ax = plt.subplot(111)

or even ax = plt.axes()

Note:

I do agree that:

fig, self.ax = plt.subplots()

is better than

fig = plt.figure()
ax = plt.subplot(111)

Copy link
Member Author

Choose a reason for hiding this comment

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

this inspired me to make #1475 to have plt.subplot() work just like plt.axes().

I'm happy to change this to just be plt.axes()

Copy link
Member

Choose a reason for hiding this comment

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

Cool. Glad it inspired you! 😄

I think we can use your new syntax here now. Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

On Tue, Nov 13, 2012 at 1:37 AM, Phil Elson notifications@github.comwrote:

Cool. Glad it inspired you! [image: 😄]

I think we can use your new syntax here now. Thoughts?

This is going into 1.2.x and the new feature was merged into master, so we
could only change this once it's merge in master.

This is the sort of bifurcation that I was talking about in the recent
thread on mpl-devel - but no solution is perfect...

self.canvas = self.ax.figure.canvas
self.cnt = 0

Expand Down
14 changes: 7 additions & 7 deletions examples/animation/old_animation/animation_blit_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
matplotlib.use('TkAgg')

import sys
import pylab as p
import matplotlib.pyplot as plt
import numpy as npy
import time

ax = p.subplot(111)
fig, ax = plt.subplots()
canvas = ax.figure.canvas


# create the initial line
x = npy.arange(0,2*npy.pi,0.01)
line, = p.plot(x, npy.sin(x), animated=True, lw=2)
line, = plt.plot(x, npy.sin(x), animated=True, lw=2)

def run(*args):
background = canvas.copy_from_bbox(ax.bbox)
Expand All @@ -43,12 +43,12 @@ def run(*args):
run.cnt = 0


p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
p.grid() # to ensure proper background restore
manager = p.get_current_fig_manager()
plt.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
plt.grid() # to ensure proper background restore
manager = plt.get_current_fig_manager()
manager.window.after(100, run)

p.show()
plt.show()



2 changes: 1 addition & 1 deletion examples/animation/old_animation/animation_blit_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
matplotlib.backends.backend_wxagg._use_accelerator(False)


ax = p.subplot(111)
fig, ax = plt.subplots()
canvas = ax.figure.canvas


Expand Down
5 changes: 2 additions & 3 deletions examples/animation/old_animation/draggable_legend.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import matplotlib.pyplot as plt


ax = plt.subplot(111)
fig, ax = plt.subplots()
ax.plot([1,2,3], label="test")

l = ax.legend()
d1 = l.draggable()

xy = 1, 2
xy = 1, 2
txt = ax.annotate("Test", xy, xytext=(-30, 30),
textcoords="offset points",
bbox=dict(boxstyle="round",fc=(0.2, 1, 1)),
Expand Down
3 changes: 1 addition & 2 deletions examples/animation/old_animation/gtk_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
line, = ax.plot(np.random.rand(10))
ax.set_ylim(0, 1)

Expand Down
3 changes: 1 addition & 2 deletions examples/animation/old_animation/histogram_tkagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import matplotlib.patches as patches
import matplotlib.path as path

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()

# histogram our data with numpy
data = np.random.randn(1000)
Expand Down
4 changes: 1 addition & 3 deletions examples/animation/old_animation/simple_anim_gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)
fig, ax = plt.subplots()

def animate():
tstart = time.time() # for profiling
Expand Down
3 changes: 1 addition & 2 deletions examples/animation/old_animation/simple_anim_tkagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
matplotlib.use('TkAgg') # do this before importing pylab

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()

def animate():
tstart = time.time() # for profiling
Expand Down
4 changes: 1 addition & 3 deletions examples/animation/old_animation/simple_idle_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)
fig, ax = plt.subplots()
t = np.arange(0, 2*np.pi, 0.1)
line, = ax.plot(t, np.sin(t))
dt = 0.05
Expand Down
4 changes: 1 addition & 3 deletions examples/animation/old_animation/simple_timer_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)
fig, ax = plt.subplots()
t = np.arange(0, 2*np.pi, 0.1)
line, = ax.plot(t, np.sin(t))
dt = 0.05
Expand Down
3 changes: 1 addition & 2 deletions examples/animation/old_animation/strip_chart_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ def update(self, *args):

from pylab import figure, show

fig = figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
scope = Scope(ax)
gobject.idle_add(scope.update)

Expand Down
3 changes: 1 addition & 2 deletions examples/animation/random_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
line, = ax.plot(np.random.rand(10))
ax.set_ylim(0, 1)

Expand Down
3 changes: 1 addition & 2 deletions examples/animation/simple_anim.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01) # x-array
line, = ax.plot(x, np.sin(x))
Expand Down
3 changes: 1 addition & 2 deletions examples/animation/strip_chart_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def emitter(p=0.03):
else:
yield np.random.rand(1)

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
scope = Scope(ax)

# pass a generator in "emitter" to produce data for the update func
Expand Down
3 changes: 1 addition & 2 deletions examples/api/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ A simple example of the recommended style is::
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111) # or add_axes
fig, ax = plt.subplots()
ax.plot(np.random.rand(10))
ax.set_xlabel('some x data')
ax.set_ylabel('some y data')
Expand Down
6 changes: 2 additions & 4 deletions examples/api/agg_oo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
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 agree with this one. (use of plt)

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @pelson. This example is for exposing the object-oriented interface. Using the pyplot state machine is discouraged.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, i don't agree with this one either, and agree with both of you, change committed.

ax.plot([1,2,3])
ax.set_title('hi mom')
ax.grid(True)
ax.set_xlabel('time')
ax.set_ylabel('volts')
canvas.print_figure('test')
fig.canvas.print_figure('test')
3 changes: 1 addition & 2 deletions examples/api/barchart_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)

womenMeans = (25, 32, 34, 20, 25)
Expand Down
Loading