Skip to content

Instantly share code, notes, and snippets.

@apnorton
Created August 28, 2015 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apnorton/4da8ac56b3a948a70078 to your computer and use it in GitHub Desktop.
Save apnorton/4da8ac56b3a948a70078 to your computer and use it in GitHub Desktop.
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