Skip to content

Axes3D.plot_surface: Allow masked arrays and NaN values #20725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1574,12 +1574,8 @@ def plot_surface(self, X, Y, Z, *args, norm=None, vmin=None,

if Z.ndim != 2:
raise ValueError("Argument Z must be 2-dimensional.")
if np.any(np.isnan(Z)):
_api.warn_external(
"Z contains NaN values. This may result in rendering "
"artifacts.")

# TODO: Support masked arrays
Z = cbook._to_unmasked_float_array(Z)
X, Y, Z = np.broadcast_arrays(X, Y, Z)
rows, cols = Z.shape

Expand Down Expand Up @@ -1655,6 +1651,27 @@ def plot_surface(self, X, Y, Z, *args, norm=None, vmin=None,
if fcolors is not None:
colset.append(fcolors[rs][cs])

# In cases where there are NaNs in the data (possibly from masked
# arrays), artifacts can be introduced. Here check whether NaNs exist
# and remove the entries if so
if not isinstance(polys, np.ndarray) or np.isnan(polys).any():
new_polys = []
new_colset = []

# Depending on fcolors, colset is either an empty list or has as
# many elements as polys. In the former case new_colset results in
# a list with None entries, that is discarded later.
for p, col in itertools.zip_longest(polys, colset):
new_poly = np.array(p)[~np.isnan(p).any(axis=1)]
if len(new_poly):
new_polys.append(new_poly)
new_colset.append(col)

# Replace previous polys and, if fcolors is not None, colset
polys = new_polys
if fcolors is not None:
colset = new_colset

# note that the striding causes some polygons to have more coordinates
# than others
polyc = art3d.Poly3DCollection(polys, *args, **kwargs)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions lib/mpl_toolkits/tests/test_mplot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,45 @@ def test_surface3d_shaded():
ax.set_zlim(-1.01, 1.01)


@mpl3d_image_comparison(['surface3d_masked.png'])
def test_surface3d_masked():
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
y = [1, 2, 3, 4, 5, 6, 7, 8]

x, y = np.meshgrid(x, y)
matrix = np.array(
[
[-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[-1, 1, 2, 3, 4, 4, 4, 3, 2, 1, 1],
[-1, -1., 4, 5, 6, 8, 6, 5, 4, 3, -1.],
[-1, -1., 7, 8, 11, 12, 11, 8, 7, -1., -1.],
[-1, -1., 8, 9, 10, 16, 10, 9, 10, 7, -1.],
[-1, -1., -1., 12, 16, 20, 16, 12, 11, -1., -1.],
[-1, -1., -1., -1., 22, 24, 22, 20, 18, -1., -1.],
[-1, -1., -1., -1., -1., 28, 26, 25, -1., -1., -1.],
]
)
z = np.ma.masked_less(matrix, 0)
norm = mcolors.Normalize(vmax=z.max(), vmin=z.min())
colors = plt.get_cmap("plasma")(norm(z))
ax.plot_surface(x, y, z, facecolors=colors)
ax.view_init(30, -80)


@mpl3d_image_comparison(['surface3d_masked_strides.png'])
def test_surface3d_masked_strides():
fig = plt.figure()
ax = fig.add_subplot(projection='3d')

x, y = np.mgrid[-6:6.1:1, -6:6.1:1]
z = np.ma.masked_less(x * y, 2)

ax.plot_surface(x, y, z, rstride=4, cstride=4)
ax.view_init(60, -45)


@mpl3d_image_comparison(['text3d.png'], remove_text=False)
def test_text3d():
fig = plt.figure()
Expand Down