问题
I'm trying to make a somewhat interactive map plot with matplotlib utilizing a button added to the toolbar in Matplotlib's navigation Toolbar.
The objective:
The objective that I'm trying to achieve is to be able to change the axes projection on the fly without creating a new axes. There are many methods in the axes object that gets created by Matplotlib to change other aspects of the plot, but I want to be able to change projection from say, PlateCarree to NorthPolarStereo and vice versa.
Some Source:
import os
import sys
import matplotlib.pyplot as plt
import matplotlib
import mpl_toolkits
import numpy as np
import cartopy
import cartopy.crs as ccrs
fig = plt.figure()
ax = plt.axes(projection=ccrs.NorthPolarStereo())
ax.stock_img()
ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61
plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
color='blue', linewidth=2, marker='o',
transform=ccrs.Geodetic(),
)
plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
color='gray', linestyle='--',
transform=ccrs.PlateCarree(),
)
ax.add_patch(matplotlib.patches.Polygon([[0,0],[20,0],[20,20],[0,20]],
fill = False,color='g',ls='--',
transform=ccrs.PlateCarree()))
ax.add_patch(matplotlib.patches.Circle([30,30],radius=10,color='g',ls='--',
transform=ccrs.PlateCarree()))
plt.text(ny_lon - 3, ny_lat - 12, 'New York',
horizontalalignment='right',
transform=ccrs.Geodetic())
plt.text(delhi_lon + 3, delhi_lat - 12, 'Delhi',
horizontalalignment='left',
transform=ccrs.Geodetic())
# ax.set_extent([-180,180,-90,90])
ax.set_global()
The problem: As can be seen, the axes is created with a projection = ccrs.NorthPolarStereo().
The projection of the axes can be obtained by executing the following:
ax.projection
Then, I try setting the projection to ccrs.PlateCarree()
ax.projection = ccrs.PlateCaree()
This alone does not update the plot, however. I have noticed that in editing some of the properties you need to draw the canvas again with
ax.figure.canvas.draw()
however, this doesn't seem to have an effect. But if I do
ax.set_global()
to set the extent to the maximum, the axes updates and changes to the correct projection... however, the data on the plot does not get updated again. Yet calling
ax.projection
indicates that the projection is now a PlateCarree projection.
How can I update the children of the axes to reflect this new projection?
I have tried
ax.update(ax.properties())
as per the matplotlib dox, however, it throws an error.
Any Ideas?
Edit:
If it is not obvious... You will need to run this in an iPython console and run those extra commands while the figure is open in order to edit it. And it must be done this way in order to achieve what I want to do. I know I can just make a new axes with a new projection, however, the end goal of this project is to maintain this axes. This is for editing and different viewing purposes that my project requires. Also, I'm stuck using matplotlib and cartopy, so no new library recommendations for plotting, please.
In reading this question I notice that I use 'however' way too often.
来源:https://stackoverflow.com/questions/60861175/how-to-update-projection-of-geoaxes-using-cartopy