matplotlib/mplot3d scatterplot respects masking but surfaceplot does not - why?

291 Views Asked by At

The source of this post is from an earlier post.

I have X,Y,Z 2D arrays. I only want to see the plot in the first-quadrant where all x, y and z are positive. I use masking on all negative z entries. However it was noticed that scatter plots indeed respect the masking and do not show the negative z-values. However surface-plots still display the negative values.

Please find below the code -

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
%matplotlib notebook

x = np.linspace(0,1.5,10)
y = np.linspace(0,1.5,10)
X,Y = np.meshgrid(x,y)
Z = 1-X-Y
Z1 = np.ma.masked_less(Z,0)
X1 = np.ma.masked_array(X,Z1.mask)
Y1 = np.ma.masked_array(Y,Z1.mask)
fig = plt.figure()

# Two plots - one scatter plot, one surface plot
ax1 = fig.add_subplot(121,projection='3d')
ax2 = fig.add_subplot(122,projection='3d')

# Scatter plot
surf1 = ax1.scatter(X1,Y1,Z1,cmap=cm.coolwarm)
ax1.set_xlim(0,1.5)
ax1.set_ylim(0,1.5)
ax1.set_zlim(0,3)
ax1.set_xlabel('x')
ax1.set_ylabel('y')


# Surface plot
surf2 = ax2.plot_surface(X1,Y1,Z1,cmap=cm.coolwarm)
ax2.set_xlim(0,1.5)
ax2.set_ylim(0,1.5)
ax2.set_zlim(0,3)
ax2.set_xlabel('x')
ax2.set_ylabel('y')


fig.colorbar(surf1, ax=ax1, location='top')
fig.colorbar(surf2, ax=ax2, location='top')
fig.savefig('Question10CompareScatterAndSurfacePlots')

Plot

The colour-bars make it evident that there are no negative z-points in the scatter plot while the surface plot stretches to negative z. Why is it so?

0

There are 0 best solutions below