3

In matplotlib 3d-plotting plot_surface even when the x, y and z axis limits are set to be >=0 the negative z-portion of the surface is still getting plotted. The same does not happen for 2d plots- if out of 20 data points provided as input for plotting, 10 fall outside the axis limits, say first-quadrant, they simply do not get displayed in the plot.

Please see the below code and corresponding plot as an evidence (code run in Jupyter notebook) -

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,100) y = np.linspace(0,1.5,100) X,Y = np.meshgrid(x,y) Z = 1-X-Y fig = plt.figure() ax = fig.add_subplot(projection='3d') surf = ax.plot_surface(X,Y,Z,cmap=cm.coolwarm) ax.set_xlim(0,1.5) ax.set_ylim(0,1.5) ax.set_zlim(0,3) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') fig.colorbar(surf, shrink=0.5, aspect=5) fig.savefig('Question10Fig') 

Above plot

The existence of the blue colour along with the colour-bar alongside makes it evident that z<0 values are present in the plot. Why is that so?

1

2 Answers 2

1

That seems to origin from

ax.set_zlim(0,3) 

Removing this line, the figure looks good to me. That seems to be a problem in matplotlib (see also this post).

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for sharing the reference post. It gave me the idea to use masking and thereby figure out a possible issue with mplot3d/matplotlib. The issue is that scatter plot respects masking while surface plots don't. I am explaining it in detail here. As for the point of removing the line ax.set_zlim(0,3) , it does not the solve the aim of not plotting portions outside of first-quadrant. It just stops the anomaly from being manifested.
@AnirbanChakraborty: Of course, removing ax.set_zlim does not solve your problem, but it seems to be the origin of the problem.
0

Add following code:

z[z < 0] = np.nan 

just using set_zlim is not enough.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.