简体   繁体   中英

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.

My idea for this was to just use a quiver plot and 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).

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''

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.

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.

The problem is that you are trying to do ax.subplot(111, projection='3d') but ax does not have any such attribute.

You need to use the correct command for adding a 3D plot

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.

Purging the old libraries and then completely reinstalling (in reality here I made a new virtual environment) made the code work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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