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

Skip to content

Commit f523d24

Browse files
committed
add_subplot(..., axes_class=...) for more idiomatic mpl_toolkits usage.
1 parent 79eca0e commit f523d24

23 files changed

+100
-95
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_subplot/add_axes gained an *axes_class* parameter
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
In particular, ``mpl_toolkits`` axes subclasses can now be idiomatically used
4+
using e.g. ``fig.add_subplot(axes_class=mpl_toolkits.axislines.Axes)``

examples/axes_grid1/demo_axes_divider.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ def demo_locatable_axes_hard(fig):
3333
divider = SubplotDivider(fig, 2, 2, 2, aspect=True)
3434

3535
# axes for image
36-
ax = Axes(fig, divider.get_position())
36+
ax = fig.add_axes(divider.get_position(), axes_class=Axes)
3737

3838
# axes for colorbar
39-
ax_cb = Axes(fig, divider.get_position())
39+
# (the label prevents Axes.add_axes from incorrectly believing that the two
40+
# axes are the same)
41+
ax_cb = fig.add_axes(divider.get_position(), axes_class=Axes, label="cb")
4042

4143
h = [Size.AxesX(ax), # main axes
4244
Size.Fixed(0.05), # padding, 0.1 inch
@@ -51,9 +53,6 @@ def demo_locatable_axes_hard(fig):
5153
ax.set_axes_locator(divider.new_locator(nx=0, ny=0))
5254
ax_cb.set_axes_locator(divider.new_locator(nx=2, ny=0))
5355

54-
fig.add_axes(ax)
55-
fig.add_axes(ax_cb)
56-
5756
ax_cb.axis["left"].toggle(all=False)
5857
ax_cb.axis["right"].toggle(ticks=True)
5958

examples/axes_grid1/parasite_simple2.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77
import matplotlib.transforms as mtransforms
88
import matplotlib.pyplot as plt
9-
from mpl_toolkits.axes_grid1.parasite_axes import SubplotHost
9+
from mpl_toolkits.axes_grid1.parasite_axes import HostAxes
1010

1111
obs = [["01_S1", 3.88, 0.14, 1970, 63],
1212
["01_S4", 5.6, 0.82, 1622, 150],
@@ -16,7 +16,7 @@
1616

1717
fig = plt.figure()
1818

19-
ax_kms = SubplotHost(fig, 1, 1, 1, aspect=1.)
19+
ax_kms = fig.add_subplot(axes_class=HostAxes, aspect=1)
2020

2121
# angular proper motion("/yr) to linear velocity(km/s) at distance=2.3kpc
2222
pm_to_kms = 1./206265.*2300*3.085e18/3.15e7/1.e5
@@ -25,8 +25,6 @@
2525
ax_pm = ax_kms.twin(aux_trans)
2626
ax_pm.set_viewlim_mode("transform")
2727

28-
fig.add_subplot(ax_kms)
29-
3028
for n, ds, dse, w, we in obs:
3129
time = ((2007 + (10. + 4/30.)/12) - 1988.5)
3230
v = ds / time * pm_to_kms

examples/axisartist/axis_direction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import mpl_toolkits.axisartist as axisartist
99

1010

11-
def setup_axes(fig, rect):
12-
ax = fig.add_axes(axisartist.Subplot(fig, rect))
11+
def setup_axes(fig, pos):
12+
ax = fig.add_subplot(pos, axes_class=axisartist.Axes)
1313

1414
ax.set_ylim(-0.1, 1.5)
1515
ax.set_yticks([0, 1])

examples/axisartist/demo_axis_direction.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ def setup_axes(fig, rect):
4343
tick_formatter1=tick_formatter1
4444
)
4545

46-
ax1 = axisartist.Subplot(fig, rect, grid_helper=grid_helper)
46+
ax1 = fig.add_subplot(
47+
rect, axes_class=axisartist.Axes, grid_helper=grid_helper)
4748
ax1.axis[:].toggle(ticklabels=False)
4849

49-
fig.add_subplot(ax1)
50-
5150
ax1.set_aspect(1.)
5251
ax1.set_xlim(-5, 12)
5352
ax1.set_ylim(-5, 10)

examples/axisartist/demo_axisline_style.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
:doc:`/gallery/ticks_and_spines/centered_spines_with_arrows` example.
1212
"""
1313

14-
from mpl_toolkits.axisartist.axislines import SubplotZero
14+
from mpl_toolkits.axisartist.axislines import AxesZero
1515
import matplotlib.pyplot as plt
1616
import numpy as np
1717

1818

1919
fig = plt.figure()
20-
ax = SubplotZero(fig, 111)
21-
fig.add_subplot(ax)
20+
ax = fig.add_subplot(axes_class=AxesZero)
2221

2322
for direction in ["xzero", "yzero"]:
2423
# adds arrows at the ends of each axis

examples/axisartist/demo_curvelinear_grid.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from matplotlib.transforms import Affine2D
1919

2020
from mpl_toolkits.axisartist import (
21-
angle_helper, Subplot, SubplotHost, ParasiteAxesAuxTrans)
21+
angle_helper, Axes, HostAxes, ParasiteAxesAuxTrans)
2222
from mpl_toolkits.axisartist.grid_helper_curvelinear import (
2323
GridHelperCurveLinear)
2424

@@ -38,14 +38,12 @@ def inv_tr(x, y):
3838

3939
grid_helper = GridHelperCurveLinear((tr, inv_tr))
4040

41-
ax1 = Subplot(fig, 1, 2, 1, grid_helper=grid_helper)
41+
ax1 = fig.add_subplot(1, 2, 1, axes_class=Axes, grid_helper=grid_helper)
4242
# ax1 will have a ticks and gridlines defined by the given
4343
# transform (+ transData of the Axes). Note that the transform of
4444
# the Axes itself (i.e., transData) is not affected by the given
4545
# transform.
4646

47-
fig.add_subplot(ax1)
48-
4947
xx, yy = tr([3, 6], [5, 10])
5048
ax1.plot(xx, yy, linewidth=2.0)
5149

@@ -84,7 +82,8 @@ def curvelinear_test2(fig):
8482
grid_helper = GridHelperCurveLinear(
8583
tr, extreme_finder=extreme_finder,
8684
grid_locator1=grid_locator1, tick_formatter1=tick_formatter1)
87-
ax1 = SubplotHost(fig, 1, 2, 2, grid_helper=grid_helper)
85+
ax1 = fig.add_subplot(
86+
1, 2, 2, axes_class=HostAxes, grid_helper=grid_helper)
8887

8988
# make ticklabels of right and top axis visible.
9089
ax1.axis["right"].major_ticklabels.set_visible(True)
@@ -94,8 +93,6 @@ def curvelinear_test2(fig):
9493
# let bottom axis shows ticklabels for 2nd coordinate (radius)
9594
ax1.axis["bottom"].get_helper().nth_coord_ticks = 1
9695

97-
fig.add_subplot(ax1)
98-
9996
ax1.set_aspect(1)
10097
ax1.set_xlim(-5, 12)
10198
ax1.set_ylim(-5, 10)

examples/axisartist/demo_curvelinear_grid2.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
GridHelperCurveLinear)
1818
from mpl_toolkits.axisartist.grid_finder import (
1919
ExtremeFinderSimple, MaxNLocator)
20-
from mpl_toolkits.axisartist.axislines import Subplot
20+
from mpl_toolkits.axisartist.axislines import Axes
2121

2222

2323
def curvelinear_test1(fig):
@@ -39,13 +39,11 @@ def inv_tr(x, y):
3939
# better tick density
4040
grid_locator1=MaxNLocator(nbins=6), grid_locator2=MaxNLocator(nbins=6))
4141

42-
ax1 = Subplot(fig, 111, grid_helper=grid_helper)
42+
ax1 = fig.add_subplot(axes_class=Axes, grid_helper=grid_helper)
4343
# ax1 will have a ticks and gridlines defined by the given
4444
# transform (+ transData of the Axes). Note that the transform of the Axes
4545
# itself (i.e., transData) is not affected by the given transform.
4646

47-
fig.add_subplot(ax1)
48-
4947
ax1.imshow(np.arange(25).reshape(5, 5),
5048
vmax=50, cmap=plt.cm.gray_r, origin="lower")
5149

examples/axisartist/demo_floating_axes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def setup_axes1(fig, rect):
3939
grid_locator1=MaxNLocator(nbins=4),
4040
grid_locator2=MaxNLocator(nbins=4))
4141

42-
ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
43-
fig.add_subplot(ax1)
42+
ax1 = fig.add_subplot(
43+
rect, axes_class=floating_axes.FloatingAxes, grid_helper=grid_helper)
4444

4545
aux_ax = ax1.get_aux_axes(tr)
4646

@@ -70,8 +70,8 @@ def setup_axes2(fig, rect):
7070
tick_formatter1=tick_formatter1,
7171
tick_formatter2=None)
7272

73-
ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
74-
fig.add_subplot(ax1)
73+
ax1 = fig.add_subplot(
74+
rect, axes_class=floating_axes.FloatingAxes, grid_helper=grid_helper)
7575

7676
# create a parasite axes whose transData in RA, cz
7777
aux_ax = ax1.get_aux_axes(tr)
@@ -114,8 +114,8 @@ def setup_axes3(fig, rect):
114114
tick_formatter1=tick_formatter1,
115115
tick_formatter2=None)
116116

117-
ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
118-
fig.add_subplot(ax1)
117+
ax1 = fig.add_subplot(
118+
rect, axes_class=floating_axes.FloatingAxes, grid_helper=grid_helper)
119119

120120
# adjust axis
121121
ax1.axis["left"].set_axis_direction("bottom")

examples/axisartist/demo_floating_axis.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import mpl_toolkits.axisartist.angle_helper as angle_helper
1515
from matplotlib.projections import PolarAxes
1616
from matplotlib.transforms import Affine2D
17-
from mpl_toolkits.axisartist import SubplotHost
17+
from mpl_toolkits.axisartist import HostAxes
1818
from mpl_toolkits.axisartist import GridHelperCurveLinear
1919

2020

@@ -42,9 +42,7 @@ def curvelinear_test2(fig):
4242
tick_formatter1=tick_formatter1
4343
)
4444

45-
ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper)
46-
47-
fig.add_subplot(ax1)
45+
ax1 = fig.add_subplot(axes_class=HostAxes, grid_helper=grid_helper)
4846

4947
# Now creates floating axis
5048

examples/axisartist/demo_parasite_axes.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
fig = plt.figure()
2525

26-
host = HostAxes(fig, [0.15, 0.1, 0.65, 0.8])
26+
host = fig.add_axes([0.15, 0.1, 0.65, 0.8], axes_class=HostAxes)
2727
par1 = ParasiteAxes(host, sharex=host)
2828
par2 = ParasiteAxes(host, sharex=host)
2929
host.parasites.append(par1)
@@ -37,8 +37,6 @@
3737

3838
par2.axis["right2"] = par2.new_fixed_axis(loc="right", offset=(60, 0))
3939

40-
fig.add_axes(host)
41-
4240
p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
4341
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
4442
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")

examples/axisartist/demo_ticklabel_alignment.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@
1010
import mpl_toolkits.axisartist as axisartist
1111

1212

13-
def setup_axes(fig, rect):
14-
ax = axisartist.Subplot(fig, rect)
15-
fig.add_subplot(ax)
16-
13+
def setup_axes(fig, pos):
14+
ax = fig.add_subplot(pos, axes_class=axisartist.Axes)
1715
ax.set_yticks([0.2, 0.8])
1816
ax.set_yticklabels(["short", "loooong"])
1917
ax.set_xticks([0.2, 0.8])
2018
ax.set_xticklabels([r"$\frac{1}{2}\pi$", r"$\pi$"])
21-
2219
return ax
2320

2421

examples/axisartist/demo_ticklabel_direction.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
import mpl_toolkits.axisartist.axislines as axislines
1010

1111

12-
def setup_axes(fig, rect):
13-
ax = axislines.Subplot(fig, rect)
14-
fig.add_subplot(ax)
15-
12+
def setup_axes(fig, pos):
13+
ax = fig.add_subplot(pos, axes_class=axislines.Axes)
1614
ax.set_yticks([0.2, 0.8])
1715
ax.set_xticks([0.2, 0.8])
18-
1916
return ax
2017

2118

examples/axisartist/simple_axis_direction01.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import mpl_toolkits.axisartist as axisartist
99

1010
fig = plt.figure(figsize=(4, 2.5))
11-
ax1 = fig.add_subplot(axisartist.Subplot(fig, 111))
11+
ax1 = fig.add_subplot(axes_class=axisartist.Axes)
1212
fig.subplots_adjust(right=0.8)
1313

1414
ax1.axis["left"].major_ticklabels.set_axis_direction("top")

examples/axisartist/simple_axis_direction03.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
import mpl_toolkits.axisartist as axisartist
1010

1111

12-
def setup_axes(fig, rect):
13-
ax = axisartist.Subplot(fig, rect)
14-
fig.add_subplot(ax)
15-
12+
def setup_axes(fig, pos):
13+
ax = fig.add_subplot(pos, axes_class=axisartist.Axes)
1614
ax.set_yticks([0.2, 0.8])
1715
ax.set_xticks([0.2, 0.8])
18-
1916
return ax
2017

2118

examples/axisartist/simple_axis_pad.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ def setup_axes(fig, rect):
4343
tick_formatter1=tick_formatter1
4444
)
4545

46-
ax1 = axisartist.Subplot(fig, rect, grid_helper=grid_helper)
46+
ax1 = fig.add_subplot(
47+
rect, axes_class=axisartist.Axes, grid_helper=grid_helper)
4748
ax1.axis[:].set_visible(False)
48-
49-
fig.add_subplot(ax1)
50-
5149
ax1.set_aspect(1.)
5250
ax1.set_xlim(-5, 12)
5351
ax1.set_ylim(-5, 10)

examples/axisartist/simple_axisartist1.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
fig = plt.figure()
1111
fig.subplots_adjust(right=0.85)
12-
ax = axisartist.Subplot(fig, 1, 1, 1)
13-
fig.add_subplot(ax)
12+
ax = fig.add_subplot(axes_class=axisartist.Axes)
1413

1514
# make some axis invisible
1615
ax.axis["bottom", "top", "right"].set_visible(False)

examples/axisartist/simple_axisline.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
"""
77

88
import matplotlib.pyplot as plt
9-
from mpl_toolkits.axisartist.axislines import SubplotZero
9+
from mpl_toolkits.axisartist.axislines import AxesZero
1010

1111

1212
fig = plt.figure()
1313
fig.subplots_adjust(right=0.85)
14-
ax = SubplotZero(fig, 1, 1, 1)
15-
fig.add_subplot(ax)
14+
ax = fig.add_subplot(axes_class=AxesZero)
1615

1716
# make right and top axis invisible
1817
ax.axis["right"].set_visible(False)

examples/axisartist/simple_axisline2.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
66
"""
77
import matplotlib.pyplot as plt
8-
from mpl_toolkits.axisartist.axislines import SubplotZero
8+
from mpl_toolkits.axisartist.axislines import AxesZero
99
import numpy as np
1010

1111
fig = plt.figure(figsize=(4, 3))
1212

1313
# a subplot with two additional axis, "xzero" and "yzero". "xzero" is
1414
# y=0 line, and "yzero" is x=0 line.
15-
ax = SubplotZero(fig, 1, 1, 1)
16-
fig.add_subplot(ax)
15+
ax = fig.add_subplot(axes_class=AxesZero)
1716

1817
# make xzero axis (horizontal axis line through y=0) visible.
1918
ax.axis["xzero"].set_visible(True)

examples/axisartist/simple_axisline3.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
66
"""
77
import matplotlib.pyplot as plt
8-
from mpl_toolkits.axisartist.axislines import Subplot
8+
from mpl_toolkits.axisartist.axislines import Axes
99

1010
fig = plt.figure(figsize=(3, 3))
1111

12-
ax = Subplot(fig, 111)
13-
fig.add_subplot(ax)
12+
ax = fig.add_subplot(axes_class=Axes)
1413

1514
ax.axis["right"].set_visible(False)
1615
ax.axis["top"].set_visible(False)

0 commit comments

Comments
 (0)