Most commands in the axes_grid1 toolkit can take an axes_class keyword argument, and the commands create an axes of the given class. For example, to create a host subplot with axisartist.Axes,
import mpl_toolkits.axisartist as AA from mpl_toolkits.axes_grid1 import host_subplot host = host_subplot(111, axes_class=AA.Axes)
To create an axes,
import mpl_toolkits.axisartist as AA fig = plt.figure() fig.add_axes([0.1, 0.1, 0.8, 0.8], axes_class=AA.Axes)
or to create a subplot
fig.add_subplot(111, axes_class=AA.Axes) # Given that 111 is the default, one can also do fig.add_subplot(axes_class=AA.Axes)
For example, you can hide the right and top spines using:
It is also possible to add a horizontal axis. For example, you may have an horizontal axis at y=0 (in data coordinate).
ax.axis["y=0"] = ax.new_floating_axis(nth_coord=0, value=0)
Or a fixed axis with some offset
# make new (right-side) yaxis, but with some offset ax.axis["right2"] = ax.new_fixed_axis(loc="right", offset=(20, 0))
You may use Matplotlib's Transform instance instead (but a inverse transformation must be defined). Often, coordinate range in a curved coordinate system may have a limited range, or may have cycles. In those cases, a more customized version of grid helper is required.
import mpl_toolkits.axisartist.angle_helper as angle_helper # PolarAxes.PolarTransform takes radian. However, we want our coordinate # system in degree tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform() # extreme finder: find a range of coordinate. # 20, 20: number of sampling points along x, y direction # The first coordinate (longitude, but theta in polar) # has a cycle of 360 degree. # The second coordinate (latitude, but radius in polar) has a minimum of 0 extreme_finder = angle_helper.ExtremeFinderCycle(20, 20, lon_cycle = 360, lat_cycle = None, lon_minmax = None, lat_minmax = (0, np.inf), ) # Find a grid values appropriate for the coordinate (degree, # minute, second). The argument is a approximate number of grids. grid_locator1 = angle_helper.LocatorDMS(12) # And also uses an appropriate formatter. Note that the acceptable Locator # and Formatter classes are different than that of Matplotlib's, and you # cannot directly use Matplotlib's Locator and Formatter here (but may be # possible in the future). tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1 )
To actually define a curvilinear coordinate, you have to use your own grid helper. A generalised version of grid helper class is supplied and this class should suffice in most of cases. A user may provide two functions which defines a transformation (and its inverse pair) from the curved coordinate to (rectilinear) image coordinate. Note that while ticks and grids are drawn for curved coordinate, the data transform of the axes itself (ax.transData) is still rectilinear (image) coordinate.
from mpl_toolkits.axisartist.grid_helper_curvelinear \ import GridHelperCurveLinear from mpl_toolkits.axisartist import Axes # from curved coordinate to rectlinear coordinate. def tr(x, y): x, y = np.asarray(x), np.asarray(y) return x, y-x # from rectlinear coordinate to curved coordinate. def inv_tr(x, y): x, y = np.asarray(x), np.asarray(y) return x, y+x grid_helper = GridHelperCurveLinear((tr, inv_tr)) fig.add_subplot(axes_class=Axes, grid_helper=grid_helper)
Again, the transData of the axes is still a rectilinear coordinate (image coordinate). You may manually do conversion between two coordinates, or you may use Parasite Axes for convenience.:
ax1 = SubplotHost(fig, 1, 2, 2, grid_helper=grid_helper) # A parasite axes with given transform ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal") # note that ax2.transData == tr + ax1.transData # Anything you draw in ax2 will match the ticks and grids of ax1. ax1.parasites.append(ax2)
AxisArtist provides a helper method to control the visibility of ticks, ticklabels, and label. To make ticklabel invisible,