#! /usr/bin/env python """ Sunspots.py is a program that reads a data file that contains the observed number of sunspots on the Sun for each month since January 1749. The file contains two columns of numbers, the first being the month and the second being the sunspot number. The program plotts the data and calculates and plots a running average of the data. Paul Eugenio Florida State University PHZ4151C 28 Jan 2019 """ from __future__ import division, print_function import matplotlib.pyplot as plt import numpy as np # number of running average data points R = 5 # Read Data File # # Note that sunspots.txt contains a two coloms of data, one column for # month number and the other for number of sunspots observed during that month. # a = np.loadtxt("sunspots.txt", float) # "a" is now a 2x2 array # slice data to obtain monthNumber and SunspotNumber arrays monthIndex, sunspotNumber = a[:,0], a[:,1] N = len(sunspotNumber) # N: length of arrays # plot raw values plt.plot(monthIndex, sunspotNumber, "y") # # Calculate running average starting from the 6th data point # and averaging from 5 previous points to the 5 following points, # 11 points total. The size of the array for the running average # will be smaller by 2*r. # runAv = np.zeros( N - 2*R, float) for i in range(len(runAv)): runAv[i] = np.average( sunspotNumber[ i: i + 2*R + 1 ] ) # plot running value results on same graph plt.plot(monthIndex[R:N-R], runAv, "b") # Setup figure for plotting 1st 1k data points # plt.title("Observed number of sunspots since January 1749") plt.xlim(0, 1000) plt.xlabel("Time (months)") plt.ylabel("Sunspot number") plt.savefig("sunspots.png") plt.show()