3d graph with dimension error

Asked

Viewed 61 times

0

I’m trying to plot a graph in 3d. I already have in 2d. All quiet there.

It is a matrix with the values of the amplitudes of a wave propagating in a tab. But I would plot the values of the matrix as 'heights'.

In the code below, the dimensions of the original matrix are len(vals) = 333 and len(vals[2]) = 116.

To the line that starts with surf = ax.plot_surface..., the error that appears is as follows:

ValueError: Shape mismatch: objects cannot be broadcast to a single shape
xx, yy = np.mgrid[1:333, 1:116]
fig = plt.figure(figsize=(13,7))
ax = plt.axes(projection='3d')
surf = ax.plot_surface(xx, yy, vals2[100], rstride=1, cstride=1, cmap='RdBu', edgecolor='none')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('Intensity')
ax.set_title('Wave propagation inside waveguide')
fig.colorbar(surf, shrink=0.5, aspect=5)  # add color bar
ax.view_init(60,35)
plt.savefig('figuraTeste3d.png', format='png')
plt.show()

1 answer

0

Arrays of x, y and z must have the same.

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

xx = np.arange(-5,5,0.25)
xx,yy = np.meshgrid(xx,xx)
zz = np.random.random(xx.shape);

fig = plt.figure(figsize=(13,7))
ax = plt.axes(projection='3d')
surf = ax.plot_surface(xx, yy, zz, rstride=1, cstride=1, cmap='RdBu', edgecolor='none')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('Intensity')
ax.set_title('Wave propagation inside waveguide')
fig.colorbar(surf, shrink=0.5, aspect=5)  # add color bar
ax.view_init(60,35)
plt.savefig('figuraTeste3d.png', format='png')
plt.show()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.