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
{{ message }}
Repository navigation
matplotlib 3D surface - gaps / holes in surface #8222
A grid is interpolated with scipy's griddata() and contains values and NaNs. However, when the grid is plotted with mplot3d's plot_surface() there are gaps in the surface (see upper plot). The lower the view height, the more of such gaps are rendered. When the grid is plotted in 2D with imshow() there are no gaps (see lower plot).
On Tue, Mar 7, 2017 at 8:19 AM, fmevers ***@***.***> wrote:
Bug report
*Bug summary*
A grid is interpolated with scipy's griddata() and contains values and
NaNs. However, when the grid is plotted with mplot3d's plot_surface()
there are gaps in the surface (see upper plot). The lower the view height,
the more of such gaps are rendered. When the grid is plotted in 2D with
imshow() there are no gaps (see lower plot).
*Code for reproduction*
import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D
Z = np.loadtxt('data.txt')
X = np.linspace(0,3,301)
Y = np.linspace(0,3,301)
X = np.array([X,]*(301)).transpose()
Y = np.array([Y,]*(301))
fig = plt.figure(figsize=(15,15), dpi=100)
ax = fig.add_subplot(2, 1, 1, projection='3d')
plt.hold(True)
ax.view_init(40,300)
surf = ax.plot_surface(X, Y, Z, cmap='jet', rstride=1, cstride=1, antialiased=False, shade=False, alpha=1.0, linewidth=0, vmin=0.25, vmax=0.35);
ax.invert_yaxis()
ax.dist = 11
ax = fig.add_subplot(2, 1, 2)
plt.hold(True)
plt.imshow(Z, cmap='jet', vmin=0.25, vmax=0.35, origin='lower');
plt.show()
*Actual outcome*
<https://camo.githubusercontent.com/41f23a4df762cff426c5c9848c83decddf50190e/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f72536a30752e706e67>
Close-up:
<https://camo.githubusercontent.com/b72cb7d99580d00453e93b5a57c434f2e2b35f1e/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f7473646b682e706e67>
*Expected outcome*
No holes in the 3D surface plot.
*Matplotlib version*
Matplotlib 1.3.0 to 1.5.3 (WinPython 3.3.2.3 to 3.5.2.3), but it was also
reproduced up to Matplotlib 2.0: http://stackoverflow.com/
questions/41268550/matplotlib-3d-surface-gaps-holes-in-surface
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#8222>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AARy-FFO4u20mblt_XgmyeAlDkatstM_ks5rjVlUgaJpZM4MVeBX>
.
Ah, I see, there are holes appearing where there shouldn't be any holes.
Well, this is a known issue with plot_surface(). It has never supported
NaNs or masked arrays, unfortunately. Yes, this is a departure from just
about every single other matplotlib function, but the nature of
plot_surface() makes it difficult to properly handle them. If you wish to
try your hand at adding support for it, I encourage you to give it a try!
On Tue, Mar 7, 2017 at 9:46 AM, Benjamin Root ***@***.***> wrote:
why would you expect no holes for the surface if the grid has NaNs?
On Tue, Mar 7, 2017 at 8:19 AM, fmevers ***@***.***> wrote:
> Bug report
>
> *Bug summary*
>
> A grid is interpolated with scipy's griddata() and contains values and
> NaNs. However, when the grid is plotted with mplot3d's plot_surface()
> there are gaps in the surface (see upper plot). The lower the view height,
> the more of such gaps are rendered. When the grid is plotted in 2D with
> imshow() there are no gaps (see lower plot).
>
> *Code for reproduction*
>
> import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D
>
> Z = np.loadtxt('data.txt')
> X = np.linspace(0,3,301)
> Y = np.linspace(0,3,301)
> X = np.array([X,]*(301)).transpose()
> Y = np.array([Y,]*(301))
> fig = plt.figure(figsize=(15,15), dpi=100)
> ax = fig.add_subplot(2, 1, 1, projection='3d')
> plt.hold(True)
> ax.view_init(40,300)
> surf = ax.plot_surface(X, Y, Z, cmap='jet', rstride=1, cstride=1, antialiased=False, shade=False, alpha=1.0, linewidth=0, vmin=0.25, vmax=0.35);
>
> ax.invert_yaxis()
> ax.dist = 11
>
> ax = fig.add_subplot(2, 1, 2)
> plt.hold(True)
> plt.imshow(Z, cmap='jet', vmin=0.25, vmax=0.35, origin='lower');
>
> plt.show()
>
> *Actual outcome*
>
>
> <https://camo.githubusercontent.com/41f23a4df762cff426c5c9848c83decddf50190e/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f72536a30752e706e67>
> Close-up:
>
>
> <https://camo.githubusercontent.com/b72cb7d99580d00453e93b5a57c434f2e2b35f1e/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f7473646b682e706e67>
>
> *Expected outcome*
>
> No holes in the 3D surface plot.
>
> *Matplotlib version*
>
> Matplotlib 1.3.0 to 1.5.3 (WinPython 3.3.2.3 to 3.5.2.3), but it was also
> reproduced up to Matplotlib 2.0: http://stackoverflow.com/quest
> ions/41268550/matplotlib-3d-surface-gaps-holes-in-surface
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <#8222>, or mute the
> thread
> <https://github.com/notifications/unsubscribe-auth/AARy-FFO4u20mblt_XgmyeAlDkatstM_ks5rjVlUgaJpZM4MVeBX>
> .
>
Did I get you right, that if I create an array with values instead of NaNs, plot_surface() would be able to handle the array correctly? This would provide me a workaround. Thank you for your encouragement! But, to be honest, adding support would exceed my coding skills by light-years.
Activity
WeatherGod commentedon Mar 7, 2017
WeatherGod commentedon Mar 7, 2017
fmevers commentedon Mar 7, 2017
Did I get you right, that if I create an array with values instead of NaNs, plot_surface() would be able to handle the array correctly? This would provide me a workaround. Thank you for your encouragement! But, to be honest, adding support would exceed my coding skills by light-years.
eric-wieser commentedon Jul 12, 2018
Can you attach the data that causes the problem?
fmevers commentedon Jul 20, 2018
This is the data set (data.txt): https://gist.github.com/anonymous/bf886433298528bfa2f0f80448ddaa6b
kbruegge commentedon Mar 7, 2019
I still have that problem with matplotlib 3.0.3. Would be happy to know about a workaround or solution.
franchesoni commentedon Oct 16, 2019
Same issue!
timhoffm commentedon Oct 24, 2019
This is indeed related to the NaN values.
If one does
Z[np.isnan(Z)] = np.nanmean(Z)the artifacts are gone.Note: Updated the example code since
plt.hold(True)is no more (and is unneeded) in matplotlib 3.1.