简体   繁体   English

pylab三维散点图,绘制数据的2d投影

[英]pylab 3d scatter plots with 2d projections of plotted data

I am trying to create a simple 3D scatter plot but I want to also show a 2D projection of this data on the same figure. 我正在尝试创建一个简单的三维散点图,但我想在同一图上显示这些数据的二维投影。 This would allow to show a correlation between two of those 3 variables that might be hard to see in a 3D plot. 这将允许显示在3D图中可能难以看到的这3个变量中的两个之间的相关性。

I remember seeing this somewhere before but was not able to find it again. 我记得之前在某个地方看过这个,但却无法再找到它。

Here is some toy example: 这是一些玩具示例:

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)

You can add 2D projections of your 3D scatter data by using the plot method and specifying zdir : 您可以使用plot方法并指定zdir来添加3D散点图数据的2D投影:

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()

在此输入图像描述

The other answer works with matplotlib 0.99, but 1.0 and later versions need something a bit different (this code checked with v1.3.1): 另一个答案适用于matplotlib 0.99,但1.0及更高版本需要一些不同的东西(此代码用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 what version of matplotlib you have by importing it and printing the version string: 您可以通过导入和打印版本字符串来查看matplotlib的版本:

import matplotlib
print matplotlib.__version__

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM