Matplotlib 在 3d 绘图面上的 2d 绘图 [英] Matplotlib 2d Plot on Faces of 3d Plot

查看:40
本文介绍了Matplotlib 在 3d 绘图面上的 2d 绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制航天器在其轨道上特定点的轨迹图.我有一段代码可以在 3dMatplotlib 中生成一个 3d 线图(我的代码和图形的一部分显示在此处(我已将 X、Y、Z 内的点数大幅减少到每个数组约 20 个以使其更容易简单复制粘贴原理一样):

I am producing plots of a spacecraft's trajectory at a specific point in its orbit. I have a piece of code which produces a 3d line plot in 3dMatplotlib (a part of mycode and figure is shown here (I have drastically reduced the number of points within X,Y,Z to ~20 per array to make it easier to simply copy and paste as the principle is the same):

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
from numpy import *

XdS=[14.54156005,  14.53922242,  14.53688586,  14.53454823, 14.5322106 ,  14.52987297, 14.52753426,  14.52519555, 14.52285792,  14.52051922,  14.51818051, 14.51584073, 14.51350095, 14.51116117, 14.5088214 , 14.50648162, 14.50414076,  14.50179991,  14.49945906,  14.49711821]
YdS=[31.13035144,  31.12920087,  31.12805245,  31.12690188, 31.12575131,  31.12460073,  31.12345016,  31.12229745, 31.12114473,  31.11999201,  31.1188393 , 31.11768443, 31.11652957,  31.11537471, 31.11421984, 31.11306283, 31.11190582,  31.11074882,  31.10959181,  31.1084348]
ZdS=[3.94109446,  3.94060316,  3.94011186,  3.93962083,  3.93912926, 3.93863796,  3.93814639,  3.93765482,  3.93716325,  3.93667169, 3.93617985,  3.93568828,  3.93519618,  3.93470434,  3.9342125 , 3.9337204 ,  3.93322829,  3.93273592,  3.93224382,  3.93175144]

fig=plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(XdS,YdS,ZdS,c='black',linewidth=2)
ax.set_xlabel('XKSM (Saturn Radii)')
ax.set_ylabel('YKSM (Saturn Radii)')
ax.set_zlabel('ZKSM (Saturn Radii)')
plt.show()

#

我想要做的是能够在该图的边缘/平面上绘制 X 与 Y、X 与 Z 和 Y 与 Z 的 2d 图,即显示 3d 轨迹在 3 中的样子2d 平面并将它们显示在当前绘图的每个轴上.(它实际上并不像听起来那么复杂,因为我已经有了轨迹的 X、Y、Z 值).在这里,我找到了一个类似的例子,它实现了这一点,但是利用了所有 3d 绘图函数,可在:http://matplotlib.org/1.3.1/examples/mplot3d/contour3d_demo3.html :如果您查看链接,它将显示我想要实现的图像类型.

#

What I want to do is be able to plot the 2d plots X vs Y, X vs Z, and Y vs Z on the edges/planes of this plot i.e. show what the 3d trajectory looks like looking at it in the 3 2d planes and display them at each axis of the current plot. (It isn’t actually as complicated as it might sound, as I already have the X,Y,Z, values for the trajectory). Here I found a similar example which achieves this, however utilising all 3d plot functions, available at: http://matplotlib.org/1.3.1/examples/mplot3d/contour3d_demo3.html : If you check out check out the link it will show the type of image i am trying to achieve.

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

fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)

ax.set_xlabel('X')
ax.set_xlim(-40, 40)
ax.set_ylabel('Y')
ax.set_ylim(-40, 40)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)

plt.show()

这在理论上正是我所需要的,因为它需要对 3d 情况进行平面视图.但是,我无法在 3d 轴上实现 2d 线图,也无法在 2d 图中使用 offset 命令(得到错误:TypeError: There is no line property "offset").

This is in theory exactly what I need, in the way it takes sort of a planar view of the 3d situation. However I cannot implement a 2d line plot on a 3d axis nor can I use the offset command in a 2d plot (getting the error: TypeError: There is no line property "offset").

是否有与 3d偏移"命令等效的 2d,是否可以按照我的意愿在 3d 绘图的平面上绘制 2d 值?还有一种方法可以绘制初始化 3d 投影的 2d 线吗?任何人都可以提供任何想法/向我指出任何方向来帮助我实现这一目标吗?

Is there a 2d equivalent to the 3d "offset" command and Is it possible to plot the 2d values on the planes of the 3d plot as I desire? Also is there a way to plot 2d lines having initialised a 3d projection? Can anyone offer any ideas/point me in any direction in general to help me achieve this?

我提前致以诚挚的谢意,如果这篇文章的任何部分出现问题,我深表歉意,这是我的第一个!

My sincere thanks in advance and apologies if any part of this post is out of order, this is my first one!

推荐答案

试试这个:

xmin = min(XdS)
ymax = max(YdS)
zmin = min(ZdS)
length_of_array = len(XdS)
xmin_array = [xmin] * length_of_array
ymax_array = [ymax] * length_of_array
zmin_array = [zmin] * length_of_array
fig=plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(XdS,YdS,ZdS,zdir='z', c='r')
ax.plot(XdS,YdS,zmin_array, zdir='z', c='g')
ax.plot(xmin_array, YdS, ZdS, 'y')
ax.plot(XdS,ymax_array,ZdS,'b')
ax.set_xlabel('XKSM (Saturn Radii)')
ax.set_ylabel('YKSM (Saturn Radii)')
ax.set_zlabel('ZKSM (Saturn Radii)')
plt.show()

这篇关于Matplotlib 在 3d 绘图面上的 2d 绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆