Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surface and 3d contour in matplotlib

Tags:

I would like to plot a surface with a colormap, wireframe and contours using matplotlib. Something like this:

enter image description here

Notice that I am not asking about the contours that lie in the plane parallel to xy but the ones that are 3D and white in the image.

If I go the naïve way and plot all these things I cannot see the contours (see code and image below).

import numpy as np from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt  fig = plt.figure() ax = fig.add_subplot(111, projection="3d") X, Y = np.mgrid[-1:1:30j, -1:1:30j] Z = np.sin(np.pi*X)*np.sin(np.pi*Y) ax.plot_surface(X, Y, Z, cmap="autumn_r", lw=0.5, rstride=1, cstride=1) ax.contour(X, Y, Z, 10, lw=3, cmap="autumn_r", linestyles="solid", offset=-1) ax.contour(X, Y, Z, 10, lw=3, colors="k", linestyles="solid") plt.show() 

enter image description here

If a add transparency to the surface facets then I can see the contours, but it looks really cluttered (see code and image below)

import numpy as np from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt  fig = plt.figure() ax = fig.add_subplot(111, projection="3d") X, Y = np.mgrid[-1:1:30j, -1:1:30j] Z = np.sin(np.pi*X)*np.sin(np.pi*Y) ax.plot_surface(X, Y, Z, cmap="autumn_r", lw=0.5, rstride=1, cstride=1, alpha=0.5) ax.contour(X, Y, Z, 10, lw=3, cmap="autumn_r", linestyles="solid", offset=-1) ax.contour(X, Y, Z, 10, lw=3, colors="k", linestyles="solid") plt.show() 

enter image description here

Question: Is there a way to obtain this result in matplotlib? The shading is not necessary, though.

like image 719
nicoguaro Avatar asked Feb 17 '16 00:02

nicoguaro


People also ask

How do I plot a 3D contour plot in Matplotlib?

Matplotlib - 3D Contour Plot. The ax.contour3D () function creates three-dimensional contour plot. It requires all the input data to be in the form of two-dimensional regular grids, with the Z-data evaluated at each point. Here, we will show a three-dimensional contour diagram of a three-dimensional sinusoidal function.

How to create a 3D surface plot in Matplotlib?

Creation of 3D Surface Plot. To create the 3-dimensional surface plot the ax.plot_surface () function is used in matplotlib. The required syntax for this function is given below: ax.plot_surface (X, Y, Z) In the above syntax, the X and Y mainly indicate a 2D array of points x and y while Z is used to indicate the 2D array of heights.

Can I superimpose a 3D contour plot on an existing surface?

Since repeated plots using the same axes will accumulate plots there's nothing stopping you from superimposing either of these 3d contour plots on your original surface.

How to make the surface opaque in Matplotlib?

The surface is made opaque by using antialiased=False. Also demonstrates using the LinearLocator and custom formatting for the z axis tick labels. import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.ticker import LinearLocator import numpy as np fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) # Make data.


1 Answers

Apparently it is a bug, if you try this

import numpy as np from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt  fig = plt.figure() ax = fig.add_subplot(111, projection="3d") X, Y = np.mgrid[-1:1:30j, -1:1:30j] Z = np.sin(np.pi*X)*np.sin(np.pi*Y)   ax.plot_surface(X, Y, Z, cmap="autumn_r", lw=0, rstride=1, cstride=1) ax.contour(X, Y, Z+1, 10, lw=3, colors="k", linestyles="solid") plt.show() 

And rotate around, you will see the contour lines disappearing when they shouldn't

like image 169
Noel Segura Meraz Avatar answered Oct 06 '22 00:10

Noel Segura Meraz