Issue
I am writing a script that generates 3D plots from an initial set of x/y values that are not a mesh grid. The script runs well and converts the data into a mesh grid and plots it fine but when I specify a cmap color the plot disappears. Why would this happen?
code:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = []
y = []
rlim = -4
llim = 4
increment = np.absolute((llim-rlim)*5)
linespace = np.array(np.linspace(rlim,llim,increment, endpoint = False))
for val in linespace:
for val1 in linespace:
x.append(val)
y.append(val1)
x = np.array(x)
y = np.array(y)
z = np.array(np.sin(np.sqrt(np.power(x,2)+np.power(y,2)))/np.sqrt(np.power(x,2)+np.power(y,2)))
rows = len(np.unique(x[~pd.isnull(x)]))
array_size = len(x)
columns = int(array_size/rows)
X = np.reshape(x, (rows, columns))
Y = np.reshape(y, (rows, columns))
Z = np.reshape(z, (rows, columns))
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap = 'Blues', edgecolor='none')
plt.show()
However if all I do is delete the cmap entry in ax.plot_surface I ge the following:
Why simply adding a cmap delete the plot?
Solution
Matplotlib is having a hard time scaling your colormap with NaN
s in the Z
matrix. I didn't look too close at your function, but it seems like you will get a NaN
at the origin (0,0)
, and it looks like 1
is a reasonable replacement. Therefore, right after Z = np.reshape(z, (rows, columns))
I added Z[np.isnan(Z)] = 1.
, resulting in the following pretty graph:
Answered By - David Kaftan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.