Skip to content

Use plt.subplots() #11580

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
Jul 5, 2018
Merged
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
2 changes: 0 additions & 2 deletions examples/lines_bars_and_markers/simple_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

# Note that using plt.subplots below is equivalent to using
# fig = plt.figure() and then ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot(t, s)

Expand Down
3 changes: 1 addition & 2 deletions examples/misc/load_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
converters={0: bytespdate2num('%d-%b-%y')},
skiprows=1, usecols=(0, 2), unpack=True)

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot_date(dates, closes, '-')
fig.autofmt_xdate()
plt.show()
3 changes: 1 addition & 2 deletions examples/pyplots/annotate_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
x = np.arange(0, 10, 0.005)
y = np.exp(-x/2.) * np.sin(2*np.pi*x)

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xlim(0, 10)
ax.set_ylim(-1, 1)
Expand Down
3 changes: 1 addition & 2 deletions examples/pyplots/auto_subplots_adjust.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"""
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot(range(10))
ax.set_yticks((2,5,7))
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))
Expand Down
3 changes: 1 addition & 2 deletions examples/ticks_and_spines/tick_labels_from_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, MaxNLocator
fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
xs = range(26)
ys = range(26)
labels = list('abcdefghijklmnopqrstuvwxyz')
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,8 +1372,7 @@ def process_selected(self, ind, xs, ys):
self.markers.set_data(xs, ys)
self.canvas.draw()

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
x, y = np.random.rand(2, 30)
line, = ax.plot(x, y, 'bs-', picker=5)

Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -3876,8 +3876,7 @@ def cross_from_below(x, threshold):
t = np.arange(0.0, 2.0, 0.1)
s = np.sin(2*np.pi*t)

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot(t, s, '-o')
ax.axhline(0.5)
ax.axhline(-0.5)
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ def test_cull_markers():
x = np.random.random(20000)
y = np.random.random(20000)

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot(x, y, 'k.')
ax.set_xlim(2, 3)

Expand Down
117 changes: 34 additions & 83 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,9 +821,7 @@ def test_axhspan_epoch():
remove_text=True, extensions=['png'])
def test_hexbin_extent():
# this test exposes sf bug 2856228
fig = plt.figure()

ax = fig.add_subplot(111)
fig, ax = plt.subplots()
data = (np.arange(2000) / 2000).reshape((2, 1000))
x, y = data

Expand All @@ -832,8 +830,7 @@ def test_hexbin_extent():
# Reuse testcase from above for a labeled data test
data = {"x": x, "y": y}

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.hexbin("x", "y", extent=[.1, .3, .6, .7], data=data)


Expand All @@ -852,9 +849,7 @@ def __init__(self, x, y):
self.x = x
self.y = y

fig = plt.figure()

ax = fig.add_subplot(111)
fig, ax = plt.subplots()
data = (np.arange(200) / 200).reshape((2, 100))
x, y = data
hb = ax.hexbin(x, y, extent=[.1, .3, .6, .7], picker=-1)
Expand All @@ -867,32 +862,29 @@ def __init__(self, x, y):
extensions=['png'])
def test_hexbin_log():
# Issue #1636
fig = plt.figure()

np.random.seed(0)
n = 100000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
y = np.power(2, y * 0.5)
ax = fig.add_subplot(111)

fig, ax = plt.subplots()
ax.hexbin(x, y, yscale='log')


def test_inverted_limits():
# Test gh:1553
# Calling invert_xaxis prior to plotting should not disable autoscaling
# while still maintaining the inverted direction
fig = plt.figure()
ax = fig.gca()
fig, ax = plt.subplots()
ax.invert_xaxis()
ax.plot([-5, -3, 2, 4], [1, 2, -3, 5])

assert ax.get_xlim() == (4, -5)
assert ax.get_ylim() == (-3, 5)
plt.close()

fig = plt.figure()
ax = fig.gca()
fig, ax = plt.subplots()
ax.invert_yaxis()
ax.plot([-5, -3, 2, 4], [1, 2, -3, 5])

Expand All @@ -908,8 +900,7 @@ def test_nonfinite_limits():
with np.errstate(divide='ignore'):
y = np.log(x)
x[len(x)//2] = np.nan
fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot(x, y)


Expand All @@ -924,9 +915,7 @@ def test_imshow():
r = np.sqrt(x**2+y**2-x*y)

# Create a contour plot at N/4 and extract both the clip path and transform
fig = plt.figure()
ax = fig.add_subplot(111)

fig, ax = plt.subplots()
ax.imshow(r)

# Reuse testcase from above for a labeled data test
Expand All @@ -948,8 +937,7 @@ def test_imshow_clip():
r = np.sqrt(x**2+y**2-x*y)

# Create a contour plot at N/4 and extract both the clip path and transform
fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()

c = ax.contour(r, [N/4])
x = c.collections[0]
Expand All @@ -970,8 +958,7 @@ def test_polycollection_joinstyle():

from matplotlib import collections as mcoll

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
verts = np.array([[1, 1], [1, 2], [2, 2], [2, 1]])
c = mcoll.PolyCollection([verts], linewidths=40)
ax.add_collection(c)
Expand All @@ -991,8 +978,7 @@ def test_polycollection_joinstyle():
]
)
def test_fill_between_input(x, y1, y2):
fig = plt.figure()
ax = fig.add_subplot(211)
fig, ax = plt.subplots()
with pytest.raises(ValueError):
ax.fill_between(x, y1, y2)

Expand All @@ -1009,8 +995,7 @@ def test_fill_between_input(x, y1, y2):
]
)
def test_fill_betweenx_input(y, x1, x2):
fig = plt.figure()
ax = fig.add_subplot(211)
fig, ax = plt.subplots()
with pytest.raises(ValueError):
ax.fill_betweenx(y, x1, x2)

Expand All @@ -1022,23 +1007,21 @@ def test_fill_between_interpolate():
y1 = np.sin(2*np.pi*x)
y2 = 1.2*np.sin(4*np.pi*x)

fig = plt.figure()
ax = fig.add_subplot(211)
ax.plot(x, y1, x, y2, color='black')
ax.fill_between(x, y1, y2, where=y2 >= y1, facecolor='white', hatch='/',
interpolate=True)
ax.fill_between(x, y1, y2, where=y2 <= y1, facecolor='red',
interpolate=True)
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.plot(x, y1, x, y2, color='black')
ax1.fill_between(x, y1, y2, where=y2 >= y1, facecolor='white', hatch='/',
interpolate=True)
ax1.fill_between(x, y1, y2, where=y2 <= y1, facecolor='red',
interpolate=True)

# Test support for masked arrays.
y2 = np.ma.masked_greater(y2, 1.0)
# Test that plotting works for masked arrays with the first element masked
y2[0] = np.ma.masked
ax1 = fig.add_subplot(212, sharex=ax)
ax1.plot(x, y1, x, y2, color='black')
ax1.fill_between(x, y1, y2, where=y2 >= y1, facecolor='green',
ax2.plot(x, y1, x, y2, color='black')
ax2.fill_between(x, y1, y2, where=y2 >= y1, facecolor='green',
interpolate=True)
ax1.fill_between(x, y1, y2, where=y2 <= y1, facecolor='red',
ax2.fill_between(x, y1, y2, where=y2 <= y1, facecolor='red',
interpolate=True)


Expand All @@ -1049,8 +1032,7 @@ def test_fill_between_interpolate_decreasing():
t = np.array([9.4, 7, 2.2])
prof = np.array([7.9, 6.6, 3.8])

fig = plt.figure(figsize=(9, 9))
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots(figsize=(9, 9))

ax.plot(t, p, 'tab:red')
ax.plot(prof, p, 'k')
Expand All @@ -1069,8 +1051,7 @@ def test_symlog():
x = np.array([0, 1, 2, 4, 6, 9, 12, 24])
y = np.array([1000000, 500000, 100000, 100, 5, 0, 0, 0])

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_yscale('symlog')
ax.set_xscale('linear')
Expand All @@ -1083,37 +1064,12 @@ def test_symlog2():
# Numbers from -50 to 50, with 0.1 as step
x = np.arange(-50, 50, 0.001)

fig = plt.figure()
ax = fig.add_subplot(511)
# Plots a simple linear function 'f(x) = x'
ax.plot(x, x)
ax.set_xscale('symlog', linthreshx=20.0)
ax.grid(True)

ax = fig.add_subplot(512)
# Plots a simple linear function 'f(x) = x'
ax.plot(x, x)
ax.set_xscale('symlog', linthreshx=2.0)
ax.grid(True)

ax = fig.add_subplot(513)
# Plots a simple linear function 'f(x) = x'
ax.plot(x, x)
ax.set_xscale('symlog', linthreshx=1.0)
ax.grid(True)

ax = fig.add_subplot(514)
# Plots a simple linear function 'f(x) = x'
ax.plot(x, x)
ax.set_xscale('symlog', linthreshx=0.1)
ax.grid(True)

ax = fig.add_subplot(515)
# Plots a simple linear function 'f(x) = x'
ax.plot(x, x)
ax.set_xscale('symlog', linthreshx=0.01)
ax.grid(True)
ax.set_ylim(-0.1, 0.1)
fig, axs = plt.subplots(5, 1)
for ax, linthreshx in zip(axs, [20., 2., 1., 0.1, 0.01]):
ax.plot(x, x)
ax.set_xscale('symlog', linthreshx=linthreshx)
ax.grid(True)
axs[-1].set_ylim(-0.1, 0.1)


def test_pcolorargs_5205():
Expand Down Expand Up @@ -1145,15 +1101,10 @@ def test_pcolormesh():
# The color array can include masked values:
Zm = ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z)

fig = plt.figure()
ax = fig.add_subplot(131)
ax.pcolormesh(Qx, Qz, Z, lw=0.5, edgecolors='k')

ax = fig.add_subplot(132)
ax.pcolormesh(Qx, Qz, Z, lw=2, edgecolors=['b', 'w'])

ax = fig.add_subplot(133)
ax.pcolormesh(Qx, Qz, Z, shading="gouraud")
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
ax1.pcolormesh(Qx, Qz, Z, lw=0.5, edgecolors='k')
ax2.pcolormesh(Qx, Qz, Z, lw=2, edgecolors=['b', 'w'])
ax3.pcolormesh(Qx, Qz, Z, shading="gouraud")


@image_comparison(baseline_images=['pcolormesh_datetime_axis'],
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@


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

x = np.linspace(0, 4 * np.pi, 50)
y = np.sin(x)
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_backends_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def _get_testable_interactive_backends():
"webagg.port_retries": 1,
})

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
ax.plot([0, 1], [2, 3])

if rcParams["backend"].startswith("GTK3"):
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ def test_remove_from_figure(use_gridspec):
"""
Test `remove_from_figure` with the specified ``use_gridspec`` setting
"""
fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
sc = ax.scatter([1, 2], [3, 4], cmap="spring")
sc.set_array(np.array([5, 6]))
pre_figbox = np.array(ax.figbox)
Expand Down
1 change: 0 additions & 1 deletion lib/matplotlib/tests/test_constrainedlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ def test_constrained_layout9():
'Test for handling suptitle and for sharex and sharey'
fig, axs = plt.subplots(2, 2, constrained_layout=True,
sharex=False, sharey=False)
# ax = fig.add_subplot(111)
for ax in axs.flatten():
pcm = example_pcolor(ax, fontsize=24)
ax.set_xlabel('')
Expand Down
Loading