问题
I am looking for help in plotting a (variable) number of filled contours onto a 3D plot. The rub is that the points need to be correctly geo-referenced. I have got the 2D case working, using Cartopy, but one can not simply use mpl_toolkits.mplot3d
, since one can only pass one projection into the figure()
method.
This question was useful, but is focused mostly on plotting a shapefile, while I have all the points and the values at each point for use in the contouring.
This question also looked promising, but does not deal with a 3D axis.
I have a method working using straight mpl_toolkits.mplot3d
, but it is distorting the data, since it is in the wrong CRS. I would use Basemap
, but it does not handle UTM projections very well for some reason.
It looks something like this though (the plot ends up much less spotted, the data forms linear features, but this should serve to give an idea of how it works):
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d Axes3D
the_data = {'grdx': range(0, 100),
'grdy': range(0, 100),
'grdz': [[np.random.rand(100) for ii in range(100)]
for jj in range(100)]}
data_heights = range(0, 300, 50)
fig = plt.figure(figsize=(17, 17))
ax = fig.add_subplot(111, projection='3d')
x = the_data['grdx']
y = the_data['grdy']
ii = 0
for height in data_heights:
print(height)
z = the_data['grdz'][ii]
shape = np.shape(z)
print(shape)
flat = np.ravel(z)
flat[np.isclose(flat, 0.5, 0.2)] = height
flat[~(flat == height)] = np.nan
z = np.reshape(flat, shape)
print(z)
ax.contourf(y, x, z, alpha=.35)
ii += 1
plt.show()
So how can I make the x and y values for the contourf()
something that cartopy can handle in 3D?
回答1:
Caveats:
- The 3d stuff in matplotlib is frequently referred to as 2.5d whenever I speak to the primary maintainer (Ben Root, @weathergod on GitHub). This should give you an indication that there are a few issues with its ability to truly render in 3d, and it seems unlikely that matplotlib will ever be able to address some of these issues (like artists having a non-constant z order). When the rendering works, it is pretty awesome. When it doesn't, there isn't a lot that can be done about it.
- Cartopy and Basemap both have hacks that allow you to visualise with the 3d mode in matplotlib. They really are hacks - YMMV, and I imagine this isn't something that is likely to go into core Basemap or Cartopy.
With that out of the way, I took my answer from Cartopy + Matplotlib (contourf) - Map Overriding data that you referenced and built-up from there.
Since you want to build on top of contours, I took the approach of having two Axes instances (and two figures). The first is the primitive 2d (cartopy) GeoAxes, the second is the non-cartopy 3D axes. Right before I do a plt.show
(or savefig), I simply close the 2d GeoAxes (with plt.close(ax)
).
Next, I use the fact that the return value from a plt.contourf is a collection of artists, from which we can take the coordinates and properties (including color) of the contours.
Using the 2d coordinates that are generated by the contourf in the 2d GeoAxes and contour collection, I insert the z dimension (the contour level) into the coordinates and construct a Poly3DCollection.
This turns out as something like:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
def f(x,y):
x, y = np.meshgrid(x, y)
return (1 - x / 2 + x**5 + y**3 + x*y**2) * np.exp(-x**2 -y**2)
nx, ny = 256, 512
X = np.linspace(-180, 10, nx)
Y = np.linspace(-90, 90, ny)
Z = f(np.linspace(-3, 3, nx), np.linspace(-3, 3, ny))
fig = plt.figure()
ax3d = fig.add_axes([0, 0, 1, 1], projection='3d')
# Make an axes that we can use for mapping the data in 2d.
proj_ax = plt.figure().add_axes([0, 0, 1, 1], projection=ccrs.Mercator())
cs = proj_ax.contourf(X, Y, Z, transform=ccrs.PlateCarree(), alpha=0.4)
for zlev, collection in zip(cs.levels, cs.collections):
paths = collection.get_paths()
# Figure out the matplotlib transform to take us from the X, Y coordinates
# to the projection coordinates.
trans_to_proj = collection.get_transform() - proj_ax.transData
paths = [trans_to_proj.transform_path(path) for path in paths]
verts3d = [np.concatenate([path.vertices,
np.tile(zlev, [path.vertices.shape[0], 1])],
axis=1)
for path in paths]
codes = [path.codes for path in paths]
pc = Poly3DCollection([])
pc.set_verts_and_codes(verts3d, codes)
# Copy all of the parameters from the contour (like colors) manually.
# Ideally we would use update_from, but that also copies things like
# the transform, and messes up the 3d plot.
pc.set_facecolor(collection.get_facecolor())
pc.set_edgecolor(collection.get_edgecolor())
pc.set_alpha(collection.get_alpha())
ax3d.add_collection3d(pc)
proj_ax.autoscale_view()
ax3d.set_xlim(*proj_ax.get_xlim())
ax3d.set_ylim(*proj_ax.get_ylim())
ax3d.set_zlim(Z.min(), Z.max())
plt.close(proj_ax.figure)
plt.show()
Of course, there is a bunch of factorisation we can do here, as well as bringing in the georeferenced component you were referring to (like having coastlines etc.).
Notice that despite the input coordinates being lat/longs, the coordinates of the 3d axes are those of a Mercator coordinate system - this tells us that we are on the right track with regards to the transforms that we are getting cartopy to do for us.
Next, I take the code from the answer you referenced to include land polygons. The matplotlib 3d axes currently has no ability to clip polygons that fall outside of the x/y limits, so I needed to do that manually.
Bringing it all together:
import cartopy.crs as ccrs
import cartopy.feature
from cartopy.mpl.patch import geos_to_path
import itertools
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from matplotlib.collections import PolyCollection
import numpy as np
def f(x,y):
x, y = np.meshgrid(x, y)
return (1 - x / 2 + x**5 + y**3 + x*y**2) * np.exp(-x**2 -y**2)
nx, ny = 256, 512
X = np.linspace(-180, 10, nx)
Y = np.linspace(-90, 90, ny)
Z = f(np.linspace(-3, 3, nx), np.linspace(-3, 3, ny))
fig = plt.figure()
ax3d = fig.add_axes([0, 0, 1, 1], projection='3d')
# Make an axes that we can use for mapping the data in 2d.
proj_ax = plt.figure().add_axes([0, 0, 1, 1], projection=ccrs.Mercator())
cs = proj_ax.contourf(X, Y, Z, transform=ccrs.PlateCarree(), alpha=0.4)
for zlev, collection in zip(cs.levels, cs.collections):
paths = collection.get_paths()
# Figure out the matplotlib transform to take us from the X, Y coordinates
# to the projection coordinates.
trans_to_proj = collection.get_transform() - proj_ax.transData
paths = [trans_to_proj.transform_path(path) for path in paths]
verts3d = [np.concatenate([path.vertices,
np.tile(zlev, [path.vertices.shape[0], 1])],
axis=1)
for path in paths]
codes = [path.codes for path in paths]
pc = Poly3DCollection([])
pc.set_verts_and_codes(verts3d, codes)
# Copy all of the parameters from the contour (like colors) manually.
# Ideally we would use update_from, but that also copies things like
# the transform, and messes up the 3d plot.
pc.set_facecolor(collection.get_facecolor())
pc.set_edgecolor(collection.get_edgecolor())
pc.set_alpha(collection.get_alpha())
ax3d.add_collection3d(pc)
proj_ax.autoscale_view()
ax3d.set_xlim(*proj_ax.get_xlim())
ax3d.set_ylim(*proj_ax.get_ylim())
ax3d.set_zlim(Z.min(), Z.max())
# Now add coastlines.
concat = lambda iterable: list(itertools.chain.from_iterable(iterable))
target_projection = proj_ax.projection
feature = cartopy.feature.NaturalEarthFeature('physical', 'land', '110m')
geoms = feature.geometries()
# Use the convenience (private) method to get the extent as a shapely geometry.
boundary = proj_ax._get_extent_geom()
# Transform the geometries from PlateCarree into the desired projection.
geoms = [target_projection.project_geometry(geom, feature.crs)
for geom in geoms]
# Clip the geometries based on the extent of the map (because mpl3d can't do it for us)
geoms = [boundary.intersection(geom) for geom in geoms]
# Convert the geometries to paths so we can use them in matplotlib.
paths = concat(geos_to_path(geom) for geom in geoms)
polys = concat(path.to_polygons() for path in paths)
lc = PolyCollection(polys, edgecolor='black',
facecolor='green', closed=True)
ax3d.add_collection3d(lc, zs=ax3d.get_zlim()[0])
plt.close(proj_ax.figure)
plt.show()
Rounding this off a bit, and abstracting a few of the concepts to functions makes this pretty useful:
import cartopy.crs as ccrs
import cartopy.feature
from cartopy.mpl.patch import geos_to_path
import itertools
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d
from matplotlib.collections import PolyCollection, LineCollection
import numpy as np
def add_contourf3d(ax, contour_set):
proj_ax = contour_set.collections[0].axes
for zlev, collection in zip(contour_set.levels, contour_set.collections):
paths = collection.get_paths()
# Figure out the matplotlib transform to take us from the X, Y
# coordinates to the projection coordinates.
trans_to_proj = collection.get_transform() - proj_ax.transData
paths = [trans_to_proj.transform_path(path) for path in paths]
verts = [path.vertices for path in paths]
codes = [path.codes for path in paths]
pc = PolyCollection([])
pc.set_verts_and_codes(verts, codes)
# Copy all of the parameters from the contour (like colors) manually.
# Ideally we would use update_from, but that also copies things like
# the transform, and messes up the 3d plot.
pc.set_facecolor(collection.get_facecolor())
pc.set_edgecolor(collection.get_edgecolor())
pc.set_alpha(collection.get_alpha())
ax3d.add_collection3d(pc, zs=zlev)
# Update the limit of the 3d axes based on the limit of the axes that
# produced the contour.
proj_ax.autoscale_view()
ax3d.set_xlim(*proj_ax.get_xlim())
ax3d.set_ylim(*proj_ax.get_ylim())
ax3d.set_zlim(Z.min(), Z.max())
def add_feature3d(ax, feature, clip_geom=None, zs=None):
"""
Add the given feature to the given axes.
"""
concat = lambda iterable: list(itertools.chain.from_iterable(iterable))
target_projection = ax.projection
geoms = list(feature.geometries())
if target_projection != feature.crs:
# Transform the geometries from the feature's CRS into the
# desired projection.
geoms = [target_projection.project_geometry(geom, feature.crs)
for geom in geoms]
if clip_geom:
# Clip the geometries based on the extent of the map (because mpl3d
# can't do it for us)
geoms = [geom.intersection(clip_geom) for geom in geoms]
# Convert the geometries to paths so we can use them in matplotlib.
paths = concat(geos_to_path(geom) for geom in geoms)
# Bug: mpl3d can't handle edgecolor='face'
kwargs = feature.kwargs
if kwargs.get('edgecolor') == 'face':
kwargs['edgecolor'] = kwargs['facecolor']
polys = concat(path.to_polygons(closed_only=False) for path in paths)
if kwargs.get('facecolor', 'none') == 'none':
lc = LineCollection(polys, **kwargs)
else:
lc = PolyCollection(polys, closed=False, **kwargs)
ax3d.add_collection3d(lc, zs=zs)
Which I used to produce the following fun 3D Robinson plot:
def f(x, y):
x, y = np.meshgrid(x, y)
return (1 - x / 2 + x**5 + y**3 + x*y**2) * np.exp(-x**2 -y**2)
nx, ny = 256, 512
X = np.linspace(-180, 10, nx)
Y = np.linspace(-89, 89, ny)
Z = f(np.linspace(-3, 3, nx), np.linspace(-3, 3, ny))
fig = plt.figure()
ax3d = fig.add_axes([0, 0, 1, 1], projection='3d')
# Make an axes that we can use for mapping the data in 2d.
proj_ax = plt.figure().add_axes([0, 0, 1, 1], projection=ccrs.Robinson())
cs = proj_ax.contourf(X, Y, Z, transform=ccrs.PlateCarree(), alpha=1)
ax3d.projection = proj_ax.projection
add_contourf3d(ax3d, cs)
# Use the convenience (private) method to get the extent as a shapely geometry.
clip_geom = proj_ax._get_extent_geom().buffer(0)
zbase = ax3d.get_zlim()[0]
add_feature3d(ax3d, cartopy.feature.OCEAN, clip_geom, zs=zbase)
add_feature3d(ax3d, cartopy.feature.LAND, clip_geom, zs=zbase)
add_feature3d(ax3d, cartopy.feature.COASTLINE, clip_geom, zs=zbase)
# Put the outline (neatline) of the projection on.
outline = cartopy.feature.ShapelyFeature(
[proj_ax.projection.boundary], proj_ax.projection,
edgecolor='black', facecolor='none')
add_feature3d(ax3d, outline, clip_geom, zs=zbase)
# Close the intermediate (2d) figure
plt.close(proj_ax.figure)
plt.show()
Answering this question was a lot of fun, and reminded me of some of the matplotlib & cartopy transform internals. There is no doubt that it has the power to produce some useful visualisations, but I personally wouldn't be using it in production due to the issues inherent with matplotlib's 3d (2.5d) implementation.
HTH
回答2:
In my environment, the error 'GEOSIntersection_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.multipolygon.MultiPolygon object at 0x1dc9e3278> was solved by simply removing the ones that causes the error
geoms2 = []
for i in range(len(geoms)) :
if geoms[i].is_valid :
geoms2.append(geoms[i])
geoms = geoms2
before intersection. The results look fine to me so far.
来源:https://stackoverflow.com/questions/48269014/contourf-in-3d-cartopy