can't plot in three dimensions

Asked 2 years ago, Updated 2 years ago, 91 views

If you try to plot a random point into 3D, an error is spewed out and cannot be resolved.
Here's the code:

xs=[]
ys = [ ]
zs = [ ]

for_in range(3):
   
    x = np.random.randn(1)
    y=np.random.randn(1)
    z=np.random.randn(1)
    
    xs.append(x)
    ys.append(y)
    zs.append(z)
    
import matplotlib.pyplot asplt
from mpl_toolkits.mplot3d import Axes 3D
config=plt.figure()
ax=Axes3D(fig)
ax.plot(xs, ys, zs, "o", color="red", ms=4, mew=0.5)

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.show()
xs, ys, zs

Next is the error.

ValueError Traceback (most recent call last)
<ipython-input-8-3315fc4fb28c>in<module>
     23fig=plt.figure()
     24ax = Axes 3D (fig)
--- 25ax.plot(xs, ys, zs, "o", color="red", ms=4, mew=0.5)
     26 
     27ax.set_xlabel('x')

ValueError: input operand has more dimensions than allowed by the axis remapping

python matplotlib

2022-09-30 20:23

1 Answers

Below is an article that describes a similar content to the source of the questionnaire, and it works fine.
[Matplotlib] Create a 3D scatterplot

The difference is that each part of the data creation is different.

To verify, instead of running each program in Python as a script file, if you start the Python interpreter and copy each program source, the program in question will fail, but the data you used to do so remains, and you can look into it.

In the source of the questionnaire, if you look at the last xs, ys, and zs display results, there are three lists of numpy.ndarray, each with only one element, in the regular Python list.It's a hybrid [3][1] two-dimensional array, so to speak.(The contents have been edited for easy viewing.)

 ([array([0.37819465]), array([1.18331264]), array([0.30752097]),
 array([1.04432989], array([1.86562421], array([1.20300503]),
 [array([0.00736387]), array([-0.05291031]), array([0.24431243]]]

For the source that works for the , if you also display the data in x, y, and z, each is a list of one-dimensional numpy.ndarray with 100 elements.

 (array([-1.20776617, 0.0755103, 0.29309606, 0.81499591, -0.04779708,
       -0.22722229,  0.1407896 ,  1.52444428, -1.1203402 ,  0.30589895,
       half-way omission
        1.124391  , -0.49878404,  1.13699793,  0.06535393,  0.3135273 ]),
 array([-0.23766362, 0.34777034, 0.37287076, 1.27270215, -0.03464486,
        1.17946645,  0.88012189, -0.87574693, -1.3405201 ,  0.21947388,
       half-way omission
       -0.72929008, -1.47974863,  1.8906365 ,  0.27601246, -0.81644342]),
 array (-0.0088914, -0.96830462, -0.80176708, 0.14194074, 1.48899668,
       -0.49330629,  0.32355049, -0.57031329,  1.12166974,  0.90732565,
       half-way omission
        0.3123933 ,  0.0106583 ,  0.39350928, -1.65329012,  0.76048113]))

One way to fix this is not to initialize Python lists or use the for loop, but to create a list of directly one-dimensional numpy.ndarray like the one that works.
The sources are summarized in three lines:

xs=np.random.randn(3)
ys=np.random.randn(3)
zs=np.random.randn(3)

Here's the result:

 (array (-0.51921527, -1.51555862, 2.63425821),
 array (-0.63553749, 1.39414176, 0.70619291],
 array (-0.24555452, -1.21405485, -2.22118169]))

Another way to fix this is to change the method used in initialization or append to numpy if you want to leave the for loop.
The source is as follows:

xs=np.empty(0)
ys = np.empty(0)
zs = np.empty(0)

for_in range(3):
   
    x = np.random.randn(1)
    y=np.random.randn(1)
    z=np.random.randn(1)
    
    xs = np.append(xs,x)
    ys = np.append(ys,y)
    zs = np.append(zs,z)

The method of making it is different, but the result will be the same as above.

 (array (0.16575579, 0.12194223, 0.95687037),
 array (-2.24153148, 0.11853497, -0.03863972),
 array([0.14752166, 0.05863792, -1.40674958]))


2022-09-30 20:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.