Skip to content
Advertisement

How to plot 3D points in Matplotlib

I have a question I have a dataset which contain 1200060 rows and 3 column. column contain points and I have to draw a 3D plot for it. I’m using the code below but I don’t know where is the error.

fig = plt.figure()
ax = plt.axes(projection="3d")
count = len(data1.index)
z_line = np.linspace(0, count, 1000)
x_line = np.cos(z_line)
y_line = np.sin(z_line)
ax.plot3D(x_line, y_line, z_line, 'gray')

z_points = count * data1[['z']]
x_points = np.cos(z_points) + 0.1 * data1[['x']]
y_points = np.sin(z_points) + 0.1 * data1[['y']]
ax.scatter3D(x_points, y_points, z_points, c=z_points, cmap='hsv')

plt.show()

error is:

ValueError: shape mismatch: objects cannot be broadcast to a single shape

I tried this one too but unsuccessful

fig = plt.figure()
ax = plt.axes(projection="3d")


x = np.linspace(len(data1['z'].index), len(data1['y'].index), len(data1['x'].index))
y = np.linspace(len(data1['z'].index), len(data1['y'].index), len(data1['x'].index))

X = data1[['x']]
Y = data1[['y']]
Z = data1[['z']]

fig = plt.figure()
ax = plt.axes(projection="3d")
ax.plot_wireframe(X, Y, Z, color='green')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.show()

this one run but didn’t show any output

and using this one same error as the first one

fig = plt.figure()
ax = plt.axes(projection="3d")

num_bars = len(data1.index)
x_pos = data1[['x']], num_bars
y_pos = data1[['y']], num_bars
z_pos = data1[['z']], num_bars
x_size = np.ones(num_bars)
y_size = np.ones(num_bars)
z_size = np.ones(num_bars)

ax.bar3d(x_pos, y_pos, z_pos, x_size, y_size, z_size, color='aqua')
plt.show()

error: ValueError: shape mismatch: objects cannot be broadcast to a single shape

Advertisement

Answer

Either use data1['z'] instead of data1[['z']], or you could even use data1[['z']].values.

Why? Because you want to work with either Series or numpy arrays here, not with DataFrames.

See the difference between:

print(type(data1))
# <class 'pandas.core.frame.DataFrame'>

print(type(data1['x']))
#<class 'pandas.core.series.Series'>

print(type(data1['x'].values))
#<class 'numpy.ndarray'>

print(type(data1[['x']]))
#<class 'pandas.core.frame.DataFrame'>

print(type(data1[['x']].values))
#<class 'numpy.ndarray'>

To be more precise, the underlying issue is that pandas does not know how it is supposed to handle the addition of two differently named columns, as can be seen here:

a = pd.DataFrame({'x':np.arange(0,5)})
b = pd.DataFrame({'y':np.arange(0,5)})
c = pd.DataFrame({'x':np.arange(0,5)})
print(a+b)
### yields:
#        x   y
#    0 NaN NaN
#    1 NaN NaN
#    2 NaN NaN
#    3 NaN NaN
#    4 NaN NaN
print(a+c)
### yields:
#       x
#    0  0
#    1  2
#    2  4
#    3  6
#    4  8
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement