简体   繁体   English

使用颤动图和Axes3D绘制3D向量的问题

[英]Issues plotting 3D vectors with the quiver plot and Axes3D

I have a vector (0,0,50) that I am rotating according to some angles around a couple of axes and then want to plot all these vectors in a 3D plot. 我有一个向量(0,0,50),该向量根据一些角度围绕几个轴旋转,然后要在3D图中绘制所有这些向量。

My idea for this was to just use a quiver plot and Axes 3D. 我的想法是仅使用颤动图和Axes 3D。

Right now I am just trying to get it working with one vector. 现在,我只是想让它与一个向量一起工作。 I am taking the rotated vector that might be, for example, (-10,30,5) and I want to always draw them from the point (0,0,0). 我正在使用旋转的矢量,例如,它可能是(-10,30,5),我想始终从点(0,0,0)绘制它们。

The Code I currently have to do this is as follows: 我目前必须执行的代码如下:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

Roted = np.array([-10,50,3]) #this will be the output of an earlier operation but it is alway in this format
VectorForPlotting = np.array([0, 0, 0, Roted[0], Roted[1], Roted[2]])
fig = plt.figure()
ax = fig.gca(111, projection='3d')
ax.quiver(VectorForPlotting[0], VectorForPlotting[1], VectorForPlotting[2], VectorForPlotting[3], VectorForPlotting[4], VectorForPlotting[5], pivot='tail')
plt.show()

Currently this just produces a blank figure box and the error: 当前,这只会产生一个空白的图形框和错误:

'AttributeError: 'Quiver' object has no attribute 'do_3d_projection'' 'AttributeError:'Quiver'对象没有属性'do_3d_projection'

From reading online it seems like for some reason it is calling quiver as if it is a 2D quiver rather than a 3D one but I cannot figure out why. 从网上阅读看来,由于某种原因,它似乎在称颤抖,好像它是2D颤抖而不是3D颤抖,但我不知道为什么。

I have also tried the line 我也试过了

ax = fig.add_subplot(111, projection='3d')

but get the same error. 但得到同样的错误。

Does anyone have any idea how to fix this or maybe an alternative way to do this? 有谁知道如何解决此问题,或者有其他解决方法?

When I first wrote the code I realised I was using matplotlib 1.5 something so I updated to the version 3x and still get this error. 当我第一次编写代码时,我意识到我正在使用matplotlib 1.5,所以我更新到了3x版本,仍然出现此错误。

The problem is that you are trying to do ax.subplot(111, projection='3d') but ax does not have any such attribute. 问题是您正在尝试执行ax.subplot(111, projection='3d')ax没有任何此类属性。

You need to use the correct command for adding a 3D plot 您需要使用正确的命令来添加3D图

fig = plt.figure() # Create a figure object
ax = fig.add_subplot(111, projection='3d') # Add a subplot to this figure object

Alternative is to use 替代方法是使用

fig = plt.figure()
ax = Axes3D(fig)

Both gives the following figure 两者都给出下图

在此处输入图片说明

In the end it was because even though I updated matplotlib to the newest version an update will not overwrite previous parts of the library that aren't changed so if anything is broken it will stay broken. 最后,这是因为即使我将matplotlib更新为最新版本,更新也不会覆盖库中未更改的先前部分,因此,如果发生任何损坏,它将保持损坏。

Purging the old libraries and then completely reinstalling (in reality here I made a new virtual environment) made the code work fine. 清除旧库,然后完全重新安装(实际上,在此我创建了一个新的虚拟环境)使代码正常工作。

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

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