I'm trying to construct a simple function that takes a subplot instance (matplotlib.axes._subplots.AxesSubplot
) and transforms its projection to another projection, for example, to one of the cartopy.crs.CRS
projections.
The idea looks something like this
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
def make_ax_map(ax, projection=ccrs.PlateCarree()):
# set ax projection to the specified projection
...
# other fancy formatting
ax2.coastlines()
...
# Create a grid of plots
fig, (ax1, ax2) = plt.subplots(ncols=2)
# the first subplot remains unchanged
ax1.plot(np.random.rand(10))
# the second one gets another projection
make_ax_map(ax2)
Of course, I can just use fig.add_subplot()
function:
fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax1.plot(np.random.rand(10))
ax2 = fig.add_subplot(122,projection=ccrs.PlateCarree())
ax2.coastlines()
but I was wondering if there is a proper matplotlib
method to change a subplot axis projection after it was defined. Reading matplotlib API didn't help unfortunately.
4 Answers 4
You can't change the projection of an existing axes, the reason is given below. However the solution to your underlying problem is simply to use the subplot_kw
argument to plt.subplots()
described in the matplotlib documentation here. For example, if you wanted all your subplots to have the cartopy.crs.PlateCarree
projection you could do
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
# Create a grid of plots
fig, (ax1, ax2) = plt.subplots(ncols=2, subplot_kw={'projection': ccrs.PlateCarree()})
Regarding the actual question, specifying a projection when you create an axes set determines the axes class you get, which is different for each projection type. For example
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
ax1 = plt.subplot(311)
ax2 = plt.subplot(312, projection='polar')
ax3 = plt.subplot(313, projection=ccrs.PlateCarree())
print(type(ax1))
print(type(ax2))
print(type(ax3))
This code will print the following
<class 'matplotlib.axes._subplots.AxesSubplot'>
<class 'matplotlib.axes._subplots.PolarAxesSubplot'>
<class 'cartopy.mpl.geoaxes.GeoAxesSubplot'>
Notice how each axes is actually an instance of a different class.
-
7Thanks! And just to confirm, the
projection
keyword determines class for all subplots at once, so there is no way to pass several projections insubplot_kw
? E.g.projection='polar'
for the first column andprojection=ccrs.PlateCarree()
for the second column of a subplot set created byplt.subplots(ncols=2)
?Denis Sergeev– Denis Sergeev01/06/2016 09:19:57Commented Jan 6, 2016 at 9:19 -
2The keywords in
subplot_kw
are passed to each axes, so I don't think you can do what you describe. Thesubplots
function is a convenience wrapper to cater for the basic use case, if you need something more you could write your own wrapper function usingadd_subplot
or similar.ajdawson– ajdawson01/07/2016 09:42:46Commented Jan 7, 2016 at 9:42 -
1Is there a way to do this with ImageGrid? It would be nice to be able to have the control over map spacing and colorbar placement that ImageGrid allows, but I don't see a way to set the projection for the subplots in the grid.Daniel Watkins– Daniel Watkins11/14/2017 00:33:22Commented Nov 14, 2017 at 0:33
-
3Yes, it is possible by using
axes_class
keyword inImageGrid
(orAxesGrid
). There is an example in cartopy's gallery. There was also a PR to cartopy (not accepted though) that can be used to create a customGeoAxesGrid
class.Denis Sergeev– Denis Sergeev11/15/2017 21:08:29Commented Nov 15, 2017 at 21:08
Assuming there are multiple axes being used for 2D plotting, like...
fig = matplotlib.pyplot.Figure()
axs = fig.subplots(3, 4) # prepare for multiple subplots
# (some plotting here)
axs[0,0].plot([1,2,3])
... one can simply destroy one of them and replace it with a new one having the 3D projection:
axs[2,3].remove()
ax = fig.add_subplot(3, 4, 12, projection='3d')
ax.plot_surface(...)
Just note that unlike rest of Python, the add_subplot
uses row-column indexing starting from 1 (not from 0).
EDIT: Changed my typo about indexing.
You can use the following function, which removes the axis and generates the axis in the specified projection, similar to dominecf answer, with the advantage that the specific subplot parameters (row, col, and index) are retrieved automatically.
import matplotlib.pyplot as plt
def update_projection(ax, axi, projection='3d', fig=None):
if fig is None:
fig = plt.gcf()
rows, cols, start, stop = axi.get_subplotspec().get_geometry()
ax.flat[start].remove()
ax.flat[start] = fig.add_subplot(rows, cols, start+1, projection=projection)
and generate a plot with all available projections
import matplotlib.projections
import numpy as np
# test data
x = np.linspace(-np.pi, np.pi, 10)
# plot all projections available
projections = matplotlib.projections.get_projection_names()
fig, ax = plt.subplots(nrows=1, ncols=len(projections), figsize=[3.5*len(projections), 4], squeeze=False)
for i, pro_i in enumerate(projections):
update_projection(ax, ax.flat[i], pro_i)
ax.flat[i].set_title(pro_i)
try:
ax.flat[i].grid(True)
ax.flat[i].plot(x, x)
except Exception as a:
print(pro_i, a)
plt.tight_layout(pad=.5)
following the answer to this question :
I found a hack to change the projection of an axe after creating it which seems to work at least in the simple example below, but I have no idea if this solution is the best way
from matplotlib.axes import Axes
from matplotlib.projections import register_projection
class CustomAxe(Axes):
name = 'customaxe'
def plotko(self, x):
self.plot(x, 'ko')
self.set_title('CustomAxe')
register_projection(CustomAxe)
if __name__ == '__main__':
import matplotlib.pyplot as plt
fig = plt.figure()
## use this syntax to create a customaxe directly
# ax = fig.add_subplot(111, projection="customaxe")
## change the projection after creation
ax = plt.gca()
ax.__class__ = CustomAxe
ax.plotko(range(10))
plt.show()