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

Skip to content

Use subplots in examples (rebase) #2043

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 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@

2012-11-09 Make plt.subplot() without arguments act as subplot(111) - PI

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-10-05 Add support for saving animations as animated GIFs. - JVDP

2012-08-11 Fix path-closing bug in patches.Polygon, so that regardless
Expand Down
8 changes: 8 additions & 0 deletions doc/users/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 call. The old way
involved a call to :func:`~matplotlib.pyplot.figure`, followed by one (or
multiple) :func:`~matplotlib.pyplot.subplot` calls.

Calling subplot() without arguments
-----------------------------------
A call to :func:`~matplotlib.pyplot.subplot` without any arguments now
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()
Copy link
Member

Choose a reason for hiding this comment

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

To me this is just as alien as the original. If you just want an axes then ax = plt.axes() is surely the only sane way of doing it.
If you need access to the figure that the axes lives on then we can make use of the circular OO design inherent throughout matplotlib and do ax.figure.

Just my 2cents.

Copy link
Member

Choose a reason for hiding this comment

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

On Fri, May 24, 2013 at 9:27 AM, Phil Elson [email protected]:

In examples/animation/old_animation/animate_decay_tk_blit.py:

@@ -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()

To me this is just as alien as the original. If you just want an axes then ax
= plt.axes() is surely the only sane way of doing it.
If you need access to the figure that the axes lives on then we can make
use of the circular OO design inherent throughout matplotlib and do
ax.figure.

Just my 2cents.

Coming from matlab, the "plt.axes()" is what looks alien to me. While
plt.subplots() isn't quite familiar to my matlab sense, at least the name
"subplot" is there and I know that is what I want. Do remember, the
examples section is intended not only to showcase the features of
matplotlib, but also serves two other purposes. 1) establish by example the
coding conventions for using the matplotlib library, 2) serve as the bridge
for migrating users away from other tools. Making the examples familiar to
them, but yet also following best coding practices of matplotlib is a
tricky balance.


Reply to this email directly or view it on GitHubhttps://github.com//pull/2043/files#r4380788
.

Copy link
Member

Choose a reason for hiding this comment

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

  1. establish by example the coding conventions for using the matplotlib library

Exactly my reason for brining this up.
I hold neither fig.add_subplot(111) nor plt.subplots() close to my heart - it seems to me that we do not want to hide from the fact that the primary plotting area inside a figure is an Axes (whether you like the name or not) so why, for newcomers reading the examples, do we not show their creation with the very helpful pyplot axes function? The matlab argument is good, indeed perhaps we could have a section introducing matplotlib for matlab users, but I don't think it should be our main driver for API design...

Making the examples familiar to them, but yet also following best coding practices of matplotlib is a tricky balance

Agreed.

line, = ax.plot([], [], animated=True, lw=2)
ax.set_ylim(-1.1, 1.1)
ax.set_xlim(0, 5)
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
16 changes: 8 additions & 8 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)
canvas = ax.figure.canvas
fig, ax = plt.subplots()
canvas = fig.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()



5 changes: 3 additions & 2 deletions examples/animation/old_animation/animation_blit_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import matplotlib
matplotlib.use('WXAgg')
matplotlib.rcParams['toolbar'] = 'None'
import matplotlib.pyplot as plt

import wx
import sys
Expand All @@ -24,8 +25,8 @@
matplotlib.backends.backend_wxagg._use_accelerator(False)


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


p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
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
4 changes: 1 addition & 3 deletions examples/animation/old_animation/histogram_tkagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
This example shows how to use a path patch to draw a bunch of
rectangles for an animated histogram
"""
import time
import numpy as np
import matplotlib
matplotlib.use('TkAgg') # do this before importing pylab
Expand All @@ -11,8 +10,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
5 changes: 1 addition & 4 deletions examples/animation/old_animation/simple_idle_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
A simple example of an animated plot using a wx backend
"""
from __future__ import print_function
import time
import numpy as np
import matplotlib
matplotlib.use('WXAgg') # do this before importing pylab

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
5 changes: 1 addition & 4 deletions examples/animation/old_animation/simple_timer_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
"""
A simple example of an animated plot using a wx backend
"""
import time
import numpy as np
import matplotlib
matplotlib.use('WXAgg') # do this before importing pylab

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
8 changes: 3 additions & 5 deletions examples/animation/old_animation/strip_chart_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import matplotlib
matplotlib.use('GTKAgg')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D


Expand Down Expand Up @@ -62,11 +63,8 @@ def update(self, *args):
return True


from pylab import figure, show

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

show()
plt.show()
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
4 changes: 1 addition & 3 deletions examples/animation/strip_chart_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Emulate an oscilloscope. Requires the animation API introduced in
matplotlib 1.0 SVN.
"""
import matplotlib
import numpy as np
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -44,8 +43,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
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