Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
if xmax is None:
xmax = max(x)
if ymax is None:
ymax = max(y)
plt.ylim(ymin, ymax)
plt.xlim(xmin, xmax)
plt.plot(x, y)
if __name__ == '__main__':
repX = []
userY = []
# Read in the CSV file
with open('RepPowerLaw.csv', newline='') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
# 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
You can’t perform that action at this time.