Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surface Plot from Image with threshold value

I want to make a surface plot from a two dimensional array, where the z-values are stored (similar to an image where the pixel values are given).

My data is an 512x512 array like:

Z
Out[85]: 
array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]])

The values of interest are in the center part of the picture.

Image where only the central circular region is to be plotted as a surface plot

I did the surface plot with:

X = np.arange(1, 513)
Y = np.arange(1, 513)
X, Y = np.meshgrid(X, Y)
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, vmin=0.01)
plt.show()

The result is:

Resulting Image without any masking or thresholding

Apart from a bad style I only want to plot the circular central region and not plotting the outer square region where all values are zero.

How can I give a threshold or a mask to the values? Or is there a much better approach? I would really appreciate an example since I am pretty new to python/numpy ect.

Thank you very much!

like image 920
triunfal Avatar asked Sep 12 '26 09:09

triunfal


1 Answers

One way to suppress plotting of zeros in your surface plot, is by replacing the zeros (or all values you do not want to plot) in your Z-Array with nans like this:

Z[Z==0]=np.nan

This should give you your the desired plot.

like image 141
P. Leibner Avatar answered Sep 14 '26 22:09

P. Leibner