10000 Add interpolation option for facecolors in plot_surface method by livlutz · Pull Request #30255 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add interpolation option for facecolors in plot_surface method #30255

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 30 additions & 3 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2138,8 +2138,10 @@
self.auto_scale_xyz([x1, x2], [y1, y2], [z1, z2], had_data)
return polyc

def plot_surface(self, X, Y, Z, *, norm=None, vmin=None,
vmax=None, lightsource=None, axlim_clip=False, **kwargs):
def plot_surface(
self, X, Y, Z, *, norm=None, vmin=None, vmax=None, lightsource=None,
axlim_clip=False, interpolation=None, **kwargs
):
"""
Create a surface plot.

Expand Down Expand Up @@ -2249,6 +2251,28 @@

fcolors = kwargs.pop('facecolors', None)

if fcolors is not None and interpolation is not None:
try:
from scipy.ndimage import zoom
except ImportError:
raise ImportError("scipy is required for interpolation of facecolors")
fc = np.asarray(fcolors)
zoom_factors = [

Check warning on line 2260 in lib/mpl_toolkits/mplot3d/axes3d.py

View check run for this annotation

Codecov / codecov/patch

lib/mpl_toolkits/mplot3d/axes3d.py#L2259-L2260

Added lines #L2259 - L2260 were not covered by tests
X.shape[0] / fc.shape[0],
X.shape[1] / fc.shape[1]
]
order = (

Check warning on line 2264 in lib/mpl_toolkits/mplot3d/axes3d.py

View check run for this annotation

Codecov / codecov/patch

lib/mpl_toolkits/mplot3d/axes3d.py#L2264

Added line #L2264 was not covered by tests
1 if interpolation == 'bilinear'
else 3 if interpolation == 'bicubic'
else 0
)
fc_interp = zoom(fc, zoom_factors, order=order)

Check warning on line 2269 in lib/mpl_toolkits/mplot3d/axes3d.py

View check run for this annotation

Codecov / codecov/patch

lib/mpl_toolkits/mplot3d/axes3d.py#L2269

Added line #L2269 was not covered by tests
if fc_interp.shape[2] not in (3, 4):
raise ValueError(

Check warning on line 2271 in lib/mpl_toolkits/mplot3d/axes3d.py

View check run for this annotation

Codecov / codecov/patch

lib/mpl_toolkits/mplot3d/axes3d.py#L2271

Added line #L2271 was not covered by tests
"Interpolated facecolors must have 3 or 4 channels (RGB or RGBA)"
)
fcolors = fc_interp.astype(float)

Check warning on line 2274 in lib/mpl_toolkits/mplot3d/axes3d.py

View check run for this annotation

Codecov / codecov/patch

lib/mpl_toolkits/mplot3d/axes3d.py#L2274

Added line #L2274 was not covered by tests

cmap = kwargs.get('cmap', None)
shade = kwargs.pop('shade', cmap is None)
if shade is None:
Expand Down Expand Up @@ -2308,7 +2332,7 @@

if fcolors is not None:
polyc = art3d.Poly3DCollection(
polys, edgecolors=colset, facecolors=colset, shade=shade,
polys, facecolors=fcolors, shade=shade,
lightsource=lightsource, axlim_clip=axlim_clip, **kwargs)
elif cmap:
polyc = art3d.Poly3DCollection(polys, axlim_clip=axlim_clip, **kwargs)
Expand Down Expand Up @@ -2503,12 +2527,15 @@
shade : bool, default: True
Whether to shade the facecolors. Shading is always disabled when
*cmap* is specified.

lightsource : `~matplotlib.colors.LightSource`, optional
The lightsource to use when *shade* is True.

axlim_clip : bool, default: False
Whether to hide patches with a vertex outside the axes view limits.

.. versionadded:: 3.10

**kwargs
All other keyword arguments are passed on to
:class:`~mpl_toolkits.mplot3d.art3d.Poly3DCollection`
Expand Down
19 changes: 19 additions & 0 deletions lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,25 @@
fig.colorbar(surf, shrink=0.5, aspect=5)


@mpl3d_image_comparison(['surface3d_facecolors_interp.png'], style='mpl20')
def test_surface3d_facecolors_interpolation():
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
X = np.linspace(-2, 2, 5)
Y = np.linspace(-2, 2, 5)
X, Y = np.meshgrid(X, Y)
Z = np.sin(X**2 + Y**2)
# Facecolors: low-res RGB grid
C = np.zeros((4, 4, 3))
C[..., 0] = np.linspace(0, 1, 4)[:, None] # Red gradient
C[..., 1] = np.linspace(1, 0, 4)[None, :] # Green gradient
C[..., 2] = 0.5 # Blue constant

# Test bicubic interpolation
ax.plot_surface(X, Y, Z, facecolors=C, interpolation='bicubic', shade=False)
ax.set_title("plot_surface with facecolors and bicubic interpolation")

Check warning on line 671 in lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

View check run for this annotation

Codecov / codecov/patch

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py#L671

Added line #L671 was not covered by tests


@image_comparison(['surface3d_label_offset_tick_position.png'], style='mpl20')
def test_surface3d_label_offset_tick_position():
plt.rcParams['axes3d.automargin'] = True # Remove when image is regenerated
Expand Down
Loading
0