Created
August 28, 2015 16:57
-
-
Save apnorton/4da8ac56b3a948a70078 to your computer and use it in GitHub Desktop.
See http://meta.math.stackexchange.com/q/21259/23353 for discussion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import matplotlib.pyplot as plt | |
from math import log | |
def make_plot(x, y, xmin=0, ymin=0, xmax=None, ymax=None, xlabel='X', ylabel='Y'): | |
# clear the figure | |
plt.clf() | |
if xmax is None: | |
xmax = max(x) | |
if ymax is None: | |
ymax = max(y) | |
plt.xlabel(xlabel) | |
plt.ylabel(ylabel) | |
plt.ylim(ymin, ymax) | |
plt.xlim(xmin, xmax) | |
plt.plot(x, y) | |
plt.show() | |
return | |
if __name__ == '__main__': | |
repX = [] | |
userY = [] | |
# Read in the CSV file | |
with open('RepPowerLaw.csv', newline='') as csvfile: | |
csvreader = csv.reader(csvfile) | |
next(csvreader) | |
for row in csvreader: | |
repX.append(float(row[0])) | |
userY.append(float(row[1])) | |
# Compute discrete derivative at x midpoint between the samples above | |
diffY = [ (userY[i+1]-userY[i])/(repX[i+1] - repX[i]) for i in range(len(userY) - 1)] | |
midX = [ (repX[i+1]+repX[i])/2 for i in range(len(userY) - 1)] | |
# Log plot values | |
logDiffY = [log(x) for x in diffY] | |
logMidX = [log(x) for x in midX] | |
# Do three plots | |
make_plot(repX, userY, xmax=8000, ymax=250000, | |
xlabel = '$R$', ylabel = '$U$') | |
make_plot(midX, diffY, xmax = 1000, ymax = 2000, | |
xlabel = '$R$', ylabel = '$dU/dR$') | |
make_plot(logMidX, logDiffY, xmin=min(logMidX), ymin=min(logDiffY), | |
xlabel = '$\log(R)$', ylabel = '$\log(dU/dR)$') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment