Search code examples
pythonplotvectorsurface

How to combine a surface and quiver plot in python in one plot


I have the following code that produces me one surface plot with a surface given as the z-coordinate with respect to a meshgrid X,Y.

This code also produces a vector field plot, as a separate figure with respect to the same coordinate system X,Y.

Is there a way for me to combine these two figures in one? Have the vector field either on the surface or at the bottom of the surface plot?

import numpy as np
import matplotlib.pyplot as plt
import grad_field
 # first create the plot of the elevation surface
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
LNG = np.linspace(lngmin,lngmax,samples1)
LAT = np.linspace(latmin,latmax, samples2)
X, Y = np.meshgrid(LNG,LAT)
ax.plot_surface(X, Y, elev_mat)
plt.show()
 
# now create the vector field plot of the gradients
[gradx,grady] = grad_field.grad_field(elev_mat)
fig1, ax1 = plt.subplots()
ax1.set_title('Arrows scale with plot width, not view')
ax1.quiver(X, Y, gradx, grady, units='xy' ,scale=2, color='red')
plt.show()

Solution

  • You can create multiple plots next/below each other via plt.subplots(nrows=1, ncols=1). Take a look at the documentation for examples.

    If you want to create multiple plots in one, you could share the x or y axis via ax.twinx() or ax.twiny().