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

Skip to content

use plt.subplots() in examples as much as possible #1458

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

Closed
wants to merge 2 commits into from
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 c6e6770cff24b1397c12080c4c27c157de05cd99
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/bayes_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def __call__(self, i):
self.line.set_data(self.x, y)
return self.line,

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ud = UpdateDist(ax, prob=0.7)
anim = FuncAnimation(fig, ud, frames=np.arange(100), init_func=ud.init,
interval=100, blit=True)
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()
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()
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