简体   繁体   中英

How to smooth the curve in python for a Beginer

I am using the following code to draw a curve from my two column Raw data ( x=time , y=|float data|).The graph it is plotting is a rough edge graph. Is it possible to have a smooth edged on these data? I am attaching the code, data and curve.

from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates
from matplotlib import style

# changing matplotlib the default style
matplotlib.style.use('ggplot')
#one of {'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}
plt.rcParams['lines.linewidth']=1
plt.rcParams['axes.facecolor']='.3'
plt.rcParams['xtick.color']='b'
plt.rcParams['ytick.color']='r'



x,y= np.loadtxt('MaxMin.txt', dtype=str, unpack=True)
x = np.array([datetime.strptime(i, "%H:%M:%S.%f") for i in x])
y = y.astype(float)

# naming the x axis 
plt.xlabel('<------Clock-Time(HH:MM:SS)------>') 
# naming the y axis 
plt.ylabel('Acceleration (m/sq.sec)') 
# giving a title to my graph 
plt.title('Sample graph!')
# plotting the points 
plt.plot(x, y)
# beautify the x-labels
plt.gcf().autofmt_xdate()
#Custom Format
loc = matplotlib.dates.MicrosecondLocator(1000000)
plt.gca().xaxis.set_major_locator(loc)
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%H:%M:%S'))
# function to show the plot 
plt.show()

I have searched similar threads but the mathematical concepts used by them went over my head. So I cannot identify what exactly has to be done for my data.

Generated Graph from RAW data

I am also giving the sample data file so that you can re-construct it at your end.

Get Data File

PS. I am also not being able to change the line color in the graph from default red even after using

plt.rcParams['lines.color']='g'

Although that is a minor issue in this case.

**Kindly use my code and data and see whether your suggestions are actually working at your end and post the output graph if possible.I am a beginner with python.So not yet fully familiar with what is very obvious with others. **

The input data has wrong timestamps, the original author should have used zero-padding when formatting the milliseconds ( %03d ).

[...]
10:27:19.3 9.50560385141
10:27:19.32 9.48882194058
10:27:19.61 9.75936468731
10:27:19.91 9.96021690527
10:27:19.122 9.48972151383
10:27:19.151 9.49265161533
[...]

We need to fix that first:

x, y = np.loadtxt('MaxMin.txt', dtype=str, unpack=True)

# fix the zero-padding issue
x_fixed = []
for xx in x:
    xs = xx.split(".")
    xs[1] = "0"*(3-len(xs[1])) + xs[1]
    x_fixed.append(xs[0] + '.' + xs[1])

x = np.array([datetime.strptime(i, "%H:%M:%S.%f") for i in x_fixed])
y = y.astype(float)

You can then use a smoothing kernel (eg moving average) to smooth the data:

window_len = 3
kernel = np.ones(window_len, dtype=float)/window_len
y_smooth = np.convolve(y, kernel, 'same')

The scipy module has some ways of getting smooth curves through your points. Try adding this to the top:

from scipy import interpolate

Then add these lines just before your plt.show() :

xnew = np.linspace(x.min(), x.max(), 100) 
bspline = interpolate.make_interp_spline(x, y)
y_smoothed = bspline(xnew)
plt.plot(xnew, y_smoothed)

If you do a little search for scipy.interpolate.make_interp_spline , you can find more info on what that does. But essentially, the combination of that and np.linspace generates a bunch of fake data points to make up a smooth curve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM