Pylab 3d scatterplots with 2d projections of plotted data

I am trying to create a simple 3D scatter plot, but I also want to show a 2D projection of this data onto the same drawing. This would show a correlation between two of these three variables, which can be difficult to see in 3D graphics.

I remember that I saw it somewhere before, but could not find it again.

Here is an example of a toy:

x= np.random.random(100) y= np.random.random(100) z= sin(x**2+y**2) fig= figure() ax= fig.add_subplot(111, projection= '3d') ax.scatter(x,y,z) 
+7
source share
3 answers

You can add 2D projections of your 3D scatter data using the plot method and specifying zdir :

 import numpy as np import matplotlib.pyplot as plt x= np.random.random(100) y= np.random.random(100) z= np.sin(3*x**2+y**2) fig= plt.figure() ax= fig.add_subplot(111, projection= '3d') ax.scatter(x,y,z) ax.plot(x, z, 'r+', zdir='y', zs=1.5) ax.plot(y, z, 'g+', zdir='x', zs=-0.5) ax.plot(x, y, 'k+', zdir='z', zs=-1.5) ax.set_xlim([-0.5, 1.5]) ax.set_ylim([-0.5, 1.5]) ax.set_zlim([-1.5, 1.5]) plt.show() 

enter image description here

+17
source

Another answer works with matplotlib 0.99, but 1.0 and later versions need something else (this code is checked with v1.3.1):

 import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D x= np.random.random(100) y= np.random.random(100) z= np.sin(3*x**2+y**2) fig= plt.figure() ax = Axes3D(fig) ax.scatter(x,y,z) ax.plot(x, z, 'r+', zdir='y', zs=1.5) ax.plot(y, z, 'g+', zdir='x', zs=-0.5) ax.plot(x, y, 'k+', zdir='z', zs=-1.5) ax.set_xlim([-0.5, 1.5]) ax.set_ylim([-0.5, 1.5]) ax.set_zlim([-1.5, 1.5]) plt.show() 

You can see which version of matplotlib you have by importing it and printing the version string:

 import matplotlib print matplotlib.__version__ 
+2
source

Hi, if I want to project the same scattered points in 3d onto a three-dimensional surface on the same graph instead of projecting on the xy yz xz plane / what is the method?

0
source

Source: https://habr.com/ru/post/987389/


All Articles