Alex Rivera | Logout

How to plot line (polygonal chain) with numpy/scipy/matplotlib with minimal smoothing

Asked 2012-10-17T13:11:00.217
10

I am trying to plot a line in matplotlib.. I am searching for the right type of interpolation.. I want something like this

taken from canvasxpress.org/line.html

where every line is smoothed. I tried several combination of scipy and matplotlib, such as

x_new = np.arange(x, x_length, 1)
tck = interpolate.splrep(x, y, s=3)
y_new = interpolate.splev(x_new, tck, der=0)
ax.plot(x_new, y_new, color+lstyle)

but the best result I get is

my result

The line represents an increasing variable.. so it is a wrong representation. What can I search for?

Thanks

Edit: I am thinking about implementing a method from myself, but I don't know if it has been already done.. pseudo code is the following

take x and y
calculate spline for each three points 
x[0], x[1], x[2] ... x[1], x[2], x[3] ... and so on
for each y[n] sums every computation done for it and divide by number of 
computations (i.e. y[1] is computed for triplette x[0..2] and x[1..3] so the 
sum is divided by two (average for each point is taken as its value)
Edit
Report

1 Answer

1

It is important to understand that the interpolation is not just a line for visualization. It is a mathematical model representing how you think the system behaves (the system which generates the data that you measured). Different types of interpolations represent different assumptions about the system.

So, if you know that your system is such that a variable can only increase, you should fit an appropriate model (i.e. use the appropriate interpolation). Looking at your data, it looks like a 2nd degree polynomial or an exponential function might fit well. A Loess (local regression) fit will also work. You can use either tailored functions like numpy.polyfit(), or generic curve fitting with scipy.optimize.curve_fit(). If you have further knowledge about the system, you should use it to select which model to fit.

answered 2012-10-17T14:28:12.917

Your Answer