Skip to content

add_subplot(..., axes_class=...) for more idiomatic mpl_toolkits usage. #18573

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
Sep 29, 2020
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
4 changes: 4 additions & 0 deletions doc/users/next_whats_new/axes_class.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_subplot/add_axes gained an *axes_class* parameter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In particular, ``mpl_toolkits`` axes subclasses can now be idiomatically used
using e.g. ``fig.add_subplot(axes_class=mpl_toolkits.axislines.Axes)``
9 changes: 4 additions & 5 deletions examples/axes_grid1/demo_axes_divider.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ def demo_locatable_axes_hard(fig):
divider = SubplotDivider(fig, 2, 2, 2, aspect=True)

# axes for image
ax = Axes(fig, divider.get_position())
ax = fig.add_axes(divider.get_position(), axes_class=Axes)

# axes for colorbar
ax_cb = Axes(fig, divider.get_position())
# (the label prevents Axes.add_axes from incorrectly believing that the two
Copy link
Contributor Author

@anntzer anntzer Sep 25, 2020

Choose a reason for hiding this comment

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

this is #10660, #9037, etc.

# axes are the same)
ax_cb = fig.add_axes(divider.get_position(), axes_class=Axes, label="cb")

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

fig.add_axes(ax)
fig.add_axes(ax_cb)

ax_cb.axis["left"].toggle(all=False)
ax_cb.axis["right"].toggle(ticks=True)

Expand Down
6 changes: 2 additions & 4 deletions examples/axes_grid1/parasite_simple2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
import matplotlib.transforms as mtransforms
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.parasite_axes import SubplotHost
from mpl_toolkits.axes_grid1.parasite_axes import HostAxes

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

fig = plt.figure()

ax_kms = SubplotHost(fig, 1, 1, 1, aspect=1.)
ax_kms = fig.add_subplot(axes_class=HostAxes, aspect=1)

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

fig.add_subplot(ax_kms)

for n, ds, dse, w, we in obs:
time = ((2007 + (10. + 4/30.)/12) - 1988.5)
v = ds / time * pm_to_kms
Expand Down
4 changes: 2 additions & 2 deletions examples/axisartist/axis_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import mpl_toolkits.axisartist as axisartist


def setup_axes(fig, rect):
ax = fig.add_axes(axisartist.Subplot(fig, rect))
def setup_axes(fig, pos):
ax = fig.add_subplot(pos, axes_class=axisartist.Axes)

ax.set_ylim(-0.1, 1.5)
ax.set_yticks([0, 1])
Expand Down
5 changes: 2 additions & 3 deletions examples/axisartist/demo_axis_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ def setup_axes(fig, rect):
tick_formatter1=tick_formatter1
)

ax1 = axisartist.Subplot(fig, rect, grid_helper=grid_helper)
ax1 = fig.add_subplot(
rect, axes_class=axisartist.Axes, grid_helper=grid_helper)
ax1.axis[:].toggle(ticklabels=False)

fig.add_subplot(ax1)

ax1.set_aspect(1.)
ax1.set_xlim(-5, 12)
ax1.set_ylim(-5, 10)
Expand Down
5 changes: 2 additions & 3 deletions examples/axisartist/demo_axisline_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
:doc:`/gallery/ticks_and_spines/centered_spines_with_arrows` example.
"""

from mpl_toolkits.axisartist.axislines import SubplotZero
from mpl_toolkits.axisartist.axislines import AxesZero
import matplotlib.pyplot as plt
import numpy as np


fig = plt.figure()
ax = SubplotZero(fig, 111)
fig.add_subplot(ax)
ax = fig.add_subplot(axes_class=AxesZero)

for direction in ["xzero", "yzero"]:
# adds arrows at the ends of each axis
Expand Down
11 changes: 4 additions & 7 deletions examples/axisartist/demo_curvelinear_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from matplotlib.transforms import Affine2D

from mpl_toolkits.axisartist import (
angle_helper, Subplot, SubplotHost, ParasiteAxesAuxTrans)
angle_helper, Axes, HostAxes, ParasiteAxesAuxTrans)
from mpl_toolkits.axisartist.grid_helper_curvelinear import (
GridHelperCurveLinear)

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

grid_helper = GridHelperCurveLinear((tr, inv_tr))

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

fig.add_subplot(ax1)

xx, yy = tr([3, 6], [5, 10])
ax1.plot(xx, yy, linewidth=2.0)

Expand Down Expand Up @@ -84,7 +82,8 @@ def curvelinear_test2(fig):
grid_helper = GridHelperCurveLinear(
tr, extreme_finder=extreme_finder,
grid_locator1=grid_locator1, tick_formatter1=tick_formatter1)
ax1 = SubplotHost(fig, 1, 2, 2, grid_helper=grid_helper)
ax1 = fig.add_subplot(
1, 2, 2, axes_class=HostAxes, grid_helper=grid_helper)

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

fig.add_subplot(ax1)

ax1.set_aspect(1)
ax1.set_xlim(-5, 12)
ax1.set_ylim(-5, 10)
Expand Down
6 changes: 2 additions & 4 deletions examples/axisartist/demo_curvelinear_grid2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
GridHelperCurveLinear)
from mpl_toolkits.axisartist.grid_finder import (
ExtremeFinderSimple, MaxNLocator)
from mpl_toolkits.axisartist.axislines import Subplot
from mpl_toolkits.axisartist.axislines import Axes


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

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

fig.add_subplot(ax1)

ax1.imshow(np.arange(25).reshape(5, 5),
vmax=50, cmap=plt.cm.gray_r, origin="lower")

Expand Down
12 changes: 6 additions & 6 deletions examples/axisartist/demo_floating_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def setup_axes1(fig, rect):
grid_locator1=MaxNLocator(nbins=4),
grid_locator2=MaxNLocator(nbins=4))

ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
fig.add_subplot(ax1)
ax1 = fig.add_subplot(
rect, axes_class=floating_axes.FloatingAxes, grid_helper=grid_helper)

aux_ax = ax1.get_aux_axes(tr)

Expand Down Expand Up @@ -70,8 +70,8 @@ def setup_axes2(fig, rect):
tick_formatter1=tick_formatter1,
tick_formatter2=None)

ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
fig.add_subplot(ax1)
ax1 = fig.add_subplot(
rect, axes_class=floating_axes.FloatingAxes, grid_helper=grid_helper)

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

ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
fig.add_subplot(ax1)
ax1 = fig.add_subplot(
rect, axes_class=floating_axes.FloatingAxes, grid_helper=grid_helper)

# adjust axis
ax1.axis["left"].set_axis_direction("bottom")
Expand Down
6 changes: 2 additions & 4 deletions examples/axisartist/demo_floating_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import mpl_toolkits.axisartist.angle_helper as angle_helper
from matplotlib.projections import PolarAxes
from matplotlib.transforms import Affine2D
from mpl_toolkits.axisartist import SubplotHost
from mpl_toolkits.axisartist import HostAxes
from mpl_toolkits.axisartist import GridHelperCurveLinear


Expand Down Expand Up @@ -42,9 +42,7 @@ def curvelinear_test2(fig):
tick_formatter1=tick_formatter1
)

ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper)

fig.add_subplot(ax1)
ax1 = fig.add_subplot(axes_class=HostAxes, grid_helper=grid_helper)

# Now creates floating axis

Expand Down
4 changes: 1 addition & 3 deletions examples/axisartist/demo_parasite_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

fig = plt.figure()

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

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

fig.add_axes(host)

p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")
Expand Down
7 changes: 2 additions & 5 deletions examples/axisartist/demo_ticklabel_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
import mpl_toolkits.axisartist as axisartist


def setup_axes(fig, rect):
ax = axisartist.Subplot(fig, rect)
fig.add_subplot(ax)

def setup_axes(fig, pos):
ax = fig.add_subplot(pos, axes_class=axisartist.Axes)
ax.set_yticks([0.2, 0.8])
ax.set_yticklabels(["short", "loooong"])
ax.set_xticks([0.2, 0.8])
ax.set_xticklabels([r"$\frac{1}{2}\pi$", r"$\pi$"])

return ax


Expand Down
7 changes: 2 additions & 5 deletions examples/axisartist/demo_ticklabel_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
import mpl_toolkits.axisartist.axislines as axislines


def setup_axes(fig, rect):
ax = axislines.Subplot(fig, rect)
fig.add_subplot(ax)

def setup_axes(fig, pos):
ax = fig.add_subplot(pos, axes_class=axislines.Axes)
ax.set_yticks([0.2, 0.8])
ax.set_xticks([0.2, 0.8])

return ax


Expand Down
2 changes: 1 addition & 1 deletion examples/axisartist/simple_axis_direction01.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import mpl_toolkits.axisartist as axisartist

fig = plt.figure(figsize=(4, 2.5))
ax1 = fig.add_subplot(axisartist.Subplot(fig, 111))
ax1 = fig.add_subplot(axes_class=axisartist.Axes)
fig.subplots_adjust(right=0.8)

ax1.axis["left"].major_ticklabels.set_axis_direction("top")
Expand Down
7 changes: 2 additions & 5 deletions examples/axisartist/simple_axis_direction03.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
import mpl_toolkits.axisartist as axisartist


def setup_axes(fig, rect):
ax = axisartist.Subplot(fig, rect)
fig.add_subplot(ax)

def setup_axes(fig, pos):
ax = fig.add_subplot(pos, axes_class=axisartist.Axes)
ax.set_yticks([0.2, 0.8])
ax.set_xticks([0.2, 0.8])

return ax


Expand Down
6 changes: 2 additions & 4 deletions examples/axisartist/simple_axis_pad.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ def setup_axes(fig, rect):
tick_formatter1=tick_formatter1
)

ax1 = axisartist.Subplot(fig, rect, grid_helper=grid_helper)
ax1 = fig.add_subplot(
rect, axes_class=axisartist.Axes, grid_helper=grid_helper)
ax1.axis[:].set_visible(False)

fig.add_subplot(ax1)

ax1.set_aspect(1.)
ax1.set_xlim(-5, 12)
ax1.set_ylim(-5, 10)
Expand Down
3 changes: 1 addition & 2 deletions examples/axisartist/simple_axisartist1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

fig = plt.figure()
fig.subplots_adjust(right=0.85)
ax = axisartist.Subplot(fig, 1, 1, 1)
fig.add_subplot(ax)
ax = fig.add_subplot(axes_class=axisartist.Axes)

# make some axis invisible
ax.axis["bottom", "top", "right"].set_visible(False)
Expand Down
5 changes: 2 additions & 3 deletions examples/axisartist/simple_axisline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
"""

import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.axislines import SubplotZero
from mpl_toolkits.axisartist.axislines import AxesZero


fig = plt.figure()
fig.subplots_adjust(right=0.85)
ax = SubplotZero(fig, 1, 1, 1)
fig.add_subplot(ax)
ax = fig.add_subplot(axes_class=AxesZero)

# make right and top axis invisible
ax.axis["right"].set_visible(False)
Expand Down
5 changes: 2 additions & 3 deletions examples/axisartist/simple_axisline2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

"""
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.axislines import SubplotZero
from mpl_toolkits.axisartist.axislines import AxesZero
import numpy as np

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

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

# make xzero axis (horizontal axis line through y=0) visible.
ax.axis["xzero"].set_visible(True)
Expand Down
5 changes: 2 additions & 3 deletions examples/axisartist/simple_axisline3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

"""
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.axislines import Subplot
from mpl_toolkits.axisartist.axislines import Axes

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

ax = Subplot(fig, 111)
fig.add_subplot(ax)
ax = fig.add_subplot(axes_class=Axes)

ax.axis["right"].set_visible(False)
ax.axis["top"].set_visible(False)
Expand Down
Loading