Rolling Average¶

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # Plot a line and use a Rolling Average to smooth oscillations.
# Dummy data with an overlaid oscillation.
x = numpy.linspace(0, 16, 256) * Angstrom
y = (0.3 * (x / Angstrom) ** 1.3 + numpy.sin(2 * numpy.pi * x / Angstrom)) * eV
# Create and tweak model.
model = Plot.PlotModel(Angstrom, eV)
model.title().setText('Plot Rolling Average')
model.xAxis().setLabel(r'$\Delta x$')
model.yAxis().setLabel(r'$\varepsilon$')
model.legend().setVisible(True)
# Create and add line.
line = Plot.Line(x, y)
line.setLabel('Line')
line.setColor('darkgreen')
line.setLineWidth(1.5)
model.addItem(line)
# Create and add rolling average with a window of 16 points.
roll = Plot.RollingAverage(window=16)
line.addItem(roll)
line.setLineStyle(':')
# Autoscale.
model.setLimits()
# Show the plot for interactive editing.
Plot.show(model)
# Save the plot (can also be saved to svg, pdf, jpeg or hdf5).
Plot.save(model, 'rolling_average.png')
|
References: Plot.PlotModel, Plot.save, Plot.Line, Plot.show, Plot.RollingAverage.
↩ Go back to Plots