Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib separate 2D contour projection plots of 3D data

I am trying to create 2D plots of the projection of 3D data. Very simply, based on this example is there a way to produce the three contour projections shown (in the X, Y, and Z directions) as separate 2D plots? I can easily create the contour projection in the Z direction by using the pyplot 'contour' command, but only the '3d' projection 'contour' command seems to be able to take the 'zdir' parameter, and it can't create 2D plots.

like image 965
GafferMan2112 Avatar asked Mar 13 '23 01:03

GafferMan2112


1 Answers

The zdir parameter in a 3D contour basically says which axis will be used to determine the contour levels in a 3D plot. In a 2D plot this is usually specified by the order of the arguments, so what we normally think of as "Z", that is, the values that determine the contour level, is put in as the third argument. In the example below, note in particular the first three arguments in each of the contourf calls:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm

X, Y, Z = axes3d.get_test_data(0.05)

plt.subplot(131)
cset = plt.contourf(X, Y, Z, cmap=cm.coolwarm)
plt.subplot(132)
cset = plt.contourf(Y, Z, X, cmap=cm.coolwarm)
plt.subplot(133)
cset = plt.contourf(X, Z, Y, cmap=cm.coolwarm)

plt.show()

enter image description here

Compared to the projections in the 3D plot:

enter image description here

Note that here I've referred to "the third argument", but there are actually lots of ways that the arguments to contourf can be interpreted, so see the docs for more details, although this approach will generally only work with calls where X, Y, and Z are explicit.

like image 160
tom10 Avatar answered Mar 24 '23 11:03

tom10