Skip to content

Instantly share code, notes, and snippets.

@Mausy5043
Last active August 29, 2015 14:19
Show Gist options
  • Save Mausy5043/9239a76e829925e7aaac to your computer and use it in GitHub Desktop.
Save Mausy5043/9239a76e829925e7aaac to your computer and use it in GitHub Desktop.
graphing example using numpy
#!/usr/bin/python
import matplotlib
matplotlib.use("Agg")
from matplotlib.dates import strpdate2num
import numpy as np
import pylab as pl
# /tmp/corrdata/txt contains comma-separated values
# YYY-MM-DD HH:MM:SS, float, float, float, etc.
C=np.loadtxt('/tmp/corrdata.txt',delimiter=',',converters={0:strpdate2num("%Y-%m-%d %H:%M:%S")})
# Values
A1 = C[:,1]
A1_extrema = [min(A1),max(A1)]
A2 = C[:,2]
# Dates
D = matplotlib.dates.num2date(C[:,0])
# Scattergraph with correlation
ab = np.polyfit(A1,A2,1)
fit = np.poly1d(ab)
r2 = np.corrcoef(A1,A2)[0,1]
print "A1 vs A2"
# fit function: y = ab[0] * x + ab[1]
# ab[0] = slope
# ab[1] = intercept
# r2 = R-squared value of the fit
print ab[0], ab[1], r2
print ""
pl.close()
pl.plot(A1,A2,'m.')
pl.plot(A1_extrema,fit(A1_extrema),'b-')
pl.title('A1 vs. A2')
pl.xlabel("A1 [-]")
pl.ylabel("A2 [-]")
pl.annotate('{0}'.format(r2) , xy=(min(A1)+0.5,fit(min(A1))), size=6 )
pl.grid(True)
pl.savefig('/tmp/graph.png')
#Trend graph
pl.close()
print "A1 & A2 trend"
print ""
pl.plot(D,A1, '.r', label='A1')
pl.plot(D,A2, '.y', label='A2')
pl.title('Trends')
pl.ylabel('value [-]')
pl.grid(True)
pl.legend(loc='upper left', prop={'size':8})
pl.gcf().autofmt_xdate()
pl.savefig('/tmp/trend.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment