0
$\begingroup$

I want to create surface plots over certain special domains, e.g., the L-shaped domain below. Currently, I am creating surface plots with matplotlib.pyplot in the typical way, i.e., creating a meshgrid, calculating the values there, setting the values outside the L-shaped domain to 0 and then using

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

In this way I obtain plots like the one on the left. However, I would like to get plots like the one on the right, where I only show the values on the L-shaped domain itself and do not have to "pad" with zero. Is there any possibility to do this with matplotlib.pyplot? Any help would be greatly appreciated.enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

You probably want to looking at masking:

https://matplotlib.org/stable/gallery/images_contours_and_fields/image_masked.html

For your L-shaped mask, it should be an adaptation of this solution:

https://stackoverflow.com/a/56229706/1132708

To make the part disappear instead of go to zero, set the value to np.nan.

$\endgroup$
3
  • $\begingroup$ Thanks for your answer. However, your second idea does sadly not work for me. If I just set the respective values to np.nan instead of 0, I receive the error message "UserWarning: Z contains NaN values. This may result in rendering artifacts." and a completely empty plot, despite the fact that only some of the values are np.nan. Any idea how to solve this? $\endgroup$
    – user136688
    Jul 28, 2022 at 5:56
  • $\begingroup$ Can you share the code you're using? $\endgroup$
    – webb
    Aug 9, 2022 at 18:59
  • 1
    $\begingroup$ Thanks for your help, webb. When I tried to prepare my code for uploading, it turned out that it was a very easy problem: This was realized as a bug in earlier versions of matplotlib. When you update matplotlib to a version > 3.5.0 it works perfectly. $\endgroup$
    – user136688
    Aug 11, 2022 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.