'制作3D散点图'ValueError:形状不匹配:对象无法广播为单个形状

问题描述 投票:0回答:1

我将使用matplotlib进行3D散点图绘制,但我遇到了一些问题。我有3个变量,每个变量的形状不同(a:70,b:144,c:3)。因此,发生值错误。如何制作3D散点图?

print(a.shape)
print(b.shape)
print(c.shape)

(70,)(144,)(3,)

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

fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(111, projection='3d') # Axe3D object

sample_size = 500
x = np.array(a)
y = np.array(b)
z = np.array(c)
ax.scatter(x, y, z, alpha=0.5, cmap=plt.cm.Greens)
plt.title("ax.scatter")
plt.show()

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-58-4590f65025fd> in <module>
     11 y = np.array(ex1_non_split_abn['ptp'])
     12 z = np.array(initial_non_split_abn['ptp'])
---> 13 ax.scatter(x, y, z, alpha=0.5, cmap=plt.cm.Greens)
     14 # plt.savefig('../../assets/images/markdown_img/180612_1225_3dplotting_scattering.svg')
     15 plt.title("ax.scatter")

~\Anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py in scatter(self, xs, ys, zs, zdir, s, c, 
depthshade, *args, **kwargs)
   2298 
   2299         xs, ys, zs = np.broadcast_arrays(
-> 2300             *[np.ravel(np.ma.filled(t, np.nan)) for t in [xs, ys, zs]])
   2301         s = np.ma.ravel(s)  # This doesn't have to match x, y in size.
   2302 

~\Anaconda3\lib\site-packages\numpy\lib\stride_tricks.py in broadcast_arrays(*args, **kwargs)
    257     args = [np.array(_m, copy=False, subok=subok) for _m in args]
    258 
--> 259     shape = _broadcast_shape(*args)
    260 
    261     if all(array.shape == shape for array in args):

~\Anaconda3\lib\site-packages\numpy\lib\stride_tricks.py in _broadcast_shape(*args)
    191     # use the old-iterator because np.nditer does not handle size 0 arrays
    192     # consistently
--> 193     b = np.broadcast(*args[:32])
    194     # unfortunately, it cannot handle 32 or more arguments directly
    195     for pos in range(32, len(args), 31):

ValueError: shape mismatch: objects cannot be broadcast to a single shape
python matplotlib 3d scatter-plot
1个回答
0
投票

您具有不同形状的变量。据我理解错误,您的第三个变量的形状为3,这可能会导致该错误。您可以尝试将形状随机增加3至32吗?

© www.soinside.com 2019 - 2024. All rights reserved.