0

I have made a 3D plot in Python using matplotlib and Axes3D. It looks pretty good, but it has a lot of jagged edges due to how much data I am plotting. I have tried scipy interpolation methods on the data, but the plot_surface command does not like the type given back. I haven't been able to find out much else on the subject.

Here is my code so far:

import numpy
import scipy.io as sio
from matplotlib import pyplot 
import matplotlib
from mpl_toolkits.mplot3d import Axes3D
import scipy

#data pulled from file
matFile = sio.loadmat(matFileLocation)
data = matFile['data']
[numrows, numcols] = numpy.shape(numpy.atleast_2d(data))
fig = pyplot.figure()
ax = fig.gca(projection = '3d')
x = range(numcols)
y = range(numrows)
X, Y = numpy.meshgrid(x,y)
hImage = ax.plot_surface(X,Y,data,cmap = 'jet', rstride = 1, cstride = 10, linewidth=0, antialiased = False)
fig.colorbar(hImage)
hImage.set_clim(mindb, maxdb)
pyplot.show()

Please note that x, y, and data will change based on different files I run. A point in the right direction would be much appreciated.

Edit: methods tried

I've tried so many different interpolation methods from too many different examples in the past two days that I can't remember what I have and haven't tried. I think I remember griddata, interp2d and some hImage.imshow(interpolation='gaussian')(or something to that effect). Griddata returned something the plot_surface couldn't understand, interp2d never finished and hImage turned my entire plot yellow with no scaled variations like I was expecting.

I have also tried other approaches like convolving my data with a 2D array of ones and dividing by the length of the 2D array. Unfortunately, my data contains a lot of very low values, so the few high values I have get lost in the convolution. Edit: I forgot to divide by len^2, the values make more sense now.

I am essentially looking for shading interp from MatLab.

SanticL
  • 56
  • 2
  • 8
  • Do you have any code showing what/how you tried to interpolate your date? Namely, which SciPy functions you tried. Also, do you have a sample file you can host on Github or something of that sort for us to try alongside you? – dblclik Aug 25 '16 at 14:16
  • Also, did you try any of the suggestions here: http://stackoverflow.com/a/35158321/1607105? – dblclik Aug 25 '16 at 14:18
  • @dblclik Yes, I started with that example, but since data is a 2D array and x and y are not, the `tck = interpolate.bisplrep(X, Y, Z, s=0)` from the example doesn't work. As for the file, I don't really have one that I can host due to work protocol. The best I can say is to make a random [140x50000] matrix (or flip flop x and y? I get mixed up between python and matlab). As for the methods I have tried, see the edit? – SanticL Aug 25 '16 at 14:43
  • Just to make sure, are you sure you're wanting to create a uniform, integer mesh? You do this here: `X, Y = numpy.meshgrid(range(numrcols),range(numrows))`, but that will create a mesh that is in the Int x Int space--is your data generated in such a way? Your mesh should conform to whatever X and Y steps you have that generated your Z data, and they don't always have to be the same array. Can you share anything about what the X, Y, and Z spaces are meant to represent in your plot? – dblclik Aug 25 '16 at 18:57

1 Answers1

0

I ended up convolving my data by a 2d array of ones and dividing by the length of the new array.

def movingaverage(interval, window_size, mindb):
    window= numpy.ones((int(window_size),int(window_size)))/(float(window_size)**2)
    return scipy.signal.convolve2d(interval, window, 'same', fillvalue=mindb)

This is a moving average type of function that I discovered while looking for other options. It smooths out my 3d plot, though it does have the trade-off of lowering high peaks because so much of my data has a low z-axis value.

SanticL
  • 56
  • 2
  • 8
  • You need to edit your code and indent the body of the defined function, as it stands now it is erroneous code and would have to be downvoted. Glad you found a solution that worked for you – dblclik Aug 25 '16 at 19:57
  • @dblclik Sorry about that! Didn't realize that it didn't take the indent with it when I copied/pasted it. Thank you for catching that. – SanticL Aug 26 '16 at 12:49
  • no problem, happens to me all of the time (I just wish someone would always remind me!) GL with your surface plots – dblclik Aug 26 '16 at 12:54
  • @dblclik Thank you! – SanticL Aug 26 '16 at 12:57