0
$\begingroup$

My picture looks like this. It shows some percent variation but, due to fluctuations, curves are not very smooth at all.

enter image description here

I would like to draw a smoother image, without altering too much the data (it is okay if the graph is not a perfect representation of the data). I am using matplotlib to draw the data, and my code looks like this:

x = df["Value"].shift(period)+1
y = 100*df["Value"].diff(period)/(period*x)

Basically, it draws the variation from the total over a period. What is the suggested way to alter the points (possibly drawing them alongside) the original data, so that the graphs do not look so jagged?

I tried just drawing every other third point, doing:

w = x[2::3]
z = y[2::3]

but I am sure there are better ways to do this. Again, this is for a nice representation only and I am mostly interested in showing a trend or any variation of it, not in showing the exact data points.

$\endgroup$
1
$\begingroup$

You could take averages piecewise. Ideally, is like sliding a window of size k on the trend, taking the average of these points (it could be 3 or 5, for example).

Alternatively, you can use exponential smoothing functions, such as savgol filter available in scipy, LOESS smoothing, or Holt-Winters smoothing. The statsmodels.tsa submodule made many smoothing techniques available.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you for suggesting to use the savgol filter. It was very easy to implement. $\endgroup$ – user Apr 5 at 13:58
  • $\begingroup$ You're right! I always use Savgol when I'm in a hurry $\endgroup$ – Leevo Apr 5 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.