You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using plot_surface with masked arrays can lead to tearing at the edges between the data and the mask.
I have created a question on StackOverflow with images of the tearing: Here's the link
Code for reproduction
# The code below is the closest I could get to artificially reproducing the effect,# I am not sure if the tearing in this example is the same kind as the one I posted to StackOverflow.%matplotlibnotebookimportmatplotlib.pyplotaspltimportnumpyasnpimportseabornassnsfrommpl_toolkits.mplot3dimportAxes3Dimportmathdefwave(x, y):
returnmath.cos(x*0.05) *math.sin(y*0.05)
data=np.zeros(shape=(256, 256))
length, height=data.shapeforxinnp.arange(length):
foryinnp.arange(height):
data[x][y] =wave(x, y)
data[(data<0.25) | (data>0.75)] =0fig=plt.figure(figsize=(3, 3))
ax=fig.add_subplot(1, 1, 1)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title("Data")
ax.imshow(data)
plt.show()
data_masked=np.ma.masked_where(data==0, data)
fig=plt.figure(figsize=(3, 3))
ax=fig.add_subplot(1, 1, 1)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title("Masked data")
ax.imshow(data_masked)
plt.show()
length, width=data.shapeX, Y=np.meshgrid(np.arange(length), np.arange(width))
fig=plt.figure(figsize=(8, 8))
ax=fig.add_subplot(1, 1, 1, projection='3d')
ax.plot_surface(
X,
Y,
data,
cmap=plt.cm.viridis,
cstride=1,
rstride=1,
linewidth=0,
antialiased=False
)
ax.set_title("Surface plot of data")
plt.show()
length, width=data.shapeX, Y=np.meshgrid(np.arange(length), np.arange(width))
fig=plt.figure(figsize=(8, 8))
ax=fig.add_subplot(1, 1, 1, projection='3d')
ax.plot_surface(
X,
Y,
data_masked,
cmap=plt.cm.viridis,
cstride=1,
rstride=1,
linewidth=0,
antialiased=False
)
ax.set_title("Surface plot of masked data")
plt.show()
Actual outcome
Expected outcome
Smooth edges at the boundaries between data and mask.
Additional information
Tearing from my real heightmap data:
Note: unlike in the code above this example has antialising on and rstride & cstride set to 1.
If I run the heightmap plot with the same parameters as the above code I get similar (albeit more blocky) results:
In both the example code and with my actual heightmap the underlying masked values are already set to 0, so if you plot the unmasked data you get a steep cliff. Perhaps this effect still comes through even if the data is masked.
Operating system
Windows 10
Matplotlib Version
3.5.1
Matplotlib Backend
nbAgg
Python version
3.9.12
Jupyter version
Jupyter Notebook 6.4.8
Installation
conda
The text was updated successfully, but these errors were encountered:
Unfortunately the stackoverflow link seems to be broken, and while I can recreate your "actual outcome" plot, I can't recreate the jagged waterfall edges on your second two plots without that data available.
As I briefly noted in my previous comment and gone into more detail in that linked ticket, a smooth boundary at a certain z level would require a rewrite of the 3D renderer, and is the topic of this earlier ticket: #8902. Going to close this ticket for now as a duplicate but please feel free to reopen if you have the data available to share!
Bug summary
Using plot_surface with masked arrays can lead to tearing at the edges between the data and the mask.
I have created a question on StackOverflow with images of the tearing: Here's the link
Code for reproduction
Actual outcome
Expected outcome
Smooth edges at the boundaries between data and mask.
Additional information
Tearing from my real heightmap data:


Note: unlike in the code above this example has antialising on and rstride & cstride set to 1.
If I run the heightmap plot with the same parameters as the above code I get similar (albeit more blocky) results:
In both the example code and with my actual heightmap the underlying masked values are already set to 0, so if you plot the unmasked data you get a steep cliff. Perhaps this effect still comes through even if the data is masked.
Operating system
Windows 10
Matplotlib Version
3.5.1
Matplotlib Backend
nbAgg
Python version
3.9.12
Jupyter version
Jupyter Notebook 6.4.8
Installation
conda
The text was updated successfully, but these errors were encountered: