1
votes

Basically, the task I want to accomplish is to produce several plots using a for loop, and then I want to combine those plots into one plot. Once I have multiple plots, I want to combine all the plots into one plot that has all the points and all the linear regression lines of my previous plots. Here is my code that I have been working on to accomplish this feat:

import math
import numpy as np
from matplotlib import pyplot as plt

uH2 = 1.90866638
uHe = 3.60187307
eH2 = 213.38
eHe = 31.96

R = float(uH2*eH2)/(uHe*eHe)

Delta = []
C_Values = []
kHeST = []
J_f21 = []
data = np.genfromtxt("Lamda_HeHCL.txt", unpack=True); 
J_i1=data[1]; 
J_f1=data[2]; 
kHe=data[7]

data = np.genfromtxt("Basecol_Basic_New_1.txt", unpack=True); 
J_i2=data[0]; 
J_f2=data[1]; 
kH2=data[5]

print kHe
print kH2

kHe = map(float, kHe)
kH2 = map(float, kH2)

kHe = np.array(kHe)
kH2= np.array(kH2)

g = len(kH2)

for i in range(20):
    for n in range(0,g):
        if J_f2[n] == i:    
                Jf21 = J_f2[n]
                J_f21.append(Jf21)
                ratio = kHe[n]/kH2[n]
                C = (((math.log(float(kH2[n]),10)))-(math.log(float(kHe[n]),10)))/math.log(R,10)
                C_Values.append(C)
                St = abs(J_f1[n] - J_i1[n])
                Delta.append(St)

fig, ax = plt.subplots()
ax.scatter(Delta,C_Values)

m, b = np.polyfit(Delta, C_Values, 1)
x = np.linspace(0, 12, 50) # both 0 and 12 are from visually inspecting the plot
plt.plot(x, np.poly1d(np.polyfit(Delta, C_Values, 1))(x), label = "y = {}*x + {}".format(m, b))
plt.xlim(0, 12)
plt.ylim(-3, 3)

for i, txt in enumerate(J_f21):
    ax.annotate(txt, (Delta[i],C_Values[i]))

plt.legend()
plt.show()

enter image description here

The above image is what the code compiles and plots. However, I want to have multiple plots. I have been trying to use this line if J_f2[n] == i:to try and have it plot multiple plots for when i = 1 to 20. Once I have that I want to combine all the plots onto one plot with an appropriate legend.

Here is a snippet of the Basecol file:

2   1   1   1   5.536e-11  6.9932e-11 7.6179e-11 7.7972e-11 7.3478e-11 6.9928e-11 5.7645e-11 5.1923e-11
3   1   1   1   2.0055e-11 2.1203e-11 2.1748e-11 2.2311e-11 2.2817e-11 2.3063e-11 2.4038e-11 2.5299e-11
3   2   1   1   1.6342e-11 2.075e-11  2.373e-11  2.8027e-11 3.2632e-11 3.5054e-11 4.3652e-11 4.9243e-11
4   1   1   1   9.307e-12  9.202e-12  9.0085e-12 8.7567e-12 8.6751e-12 8.723e-12  9.2386e-12 9.6497e-12
4   2   1   1   1.0556e-11 1.135e-11  1.1889e-11 1.2677e-11 1.3755e-11 1.4523e-11 1.8932e-11 2.3811e-11
4   3   1   1   7.649e-12  8.4137e-12 9.3186e-12 1.1427e-11 1.4925e-11 1.7296e-11 2.8093e-11 3.6589e-11
5   1   1   1   4.9158e-13 4.7265e-13 4.8109e-13 5.3278e-13 6.5653e-13 7.6023e-13 1.4127e-12 2.1105e-12
5   2   1   1   1.0458e-11 1.0189e-11 1.003e-11  0.998e-11  1.026e-11  1.0529e-11 1.1983e-11 1.3233e-11
5   3   1   1   4.691e-12  4.9662e-12 5.2767e-12 5.9551e-12 7.0047e-12 7.76e-12   1.2318e-11 1.7668e-11
5   4   1   1   1.1144e-11 1.0575e-11 1.0487e-11 1.1112e-11 1.3046e-11 1.4654e-11 2.3451e-11 3.1304e-11
6   1   1   1   2.3703e-13 2.3679e-13 2.3847e-13 2.5019e-13 2.8183e-13 3.1013e-13 5.3148e-13 8.7515e-13
6   2   1   1   8.4873e-13 8.2252e-13 8.2998e-13 8.9342e-13 1.0548e-12 1.1898e-12 2.0178e-12 2.8894e-12

Here is a snippet of the lamda:

    1    2   1    4.151E-12 4.553E-12 4.600E-12 4.852E-12 6.173E-12 7.756E-12 9.383E-12 1.096E-11 1.243E-11 1.379E-11 1.504E-11 1.619E-11 1.724E-11 2.139E-11 2.426E-11 2.791E-11 3.009E-11 3.152E-11 3.252E-11 3.326E-11 3.382E-11 3.426E-11 3.462E-11 3.572E-11 3.640E-11 3.698E-11 3.752E-11
    2    3   1    1.433E-12 1.655E-12 1.907E-12 2.014E-12 2.282E-12 2.682E-12 3.159E-12 3.685E-12 4.246E-12 4.833E-12 5.440E-12 6.059E-12 6.688E-12 9.845E-12 1.285E-11 1.810E-11 2.238E-11 2.590E-11 2.886E-11 3.139E-11 3.359E-11 3.552E-11 3.724E-11 4.375E-11 4.832E-11 5.192E-11 5.486E-11
    3    3   2    1.451E-11 1.661E-11 1.839E-11 1.828E-11 1.833E-11 1.904E-11 2.002E-11 2.111E-11 2.223E-11 2.334E-11 2.442E-11 2.546E-11 2.646E-11 3.071E-11 3.397E-11 3.860E-11 4.176E-11 4.407E-11 4.585E-11 4.728E-11 4.845E-11 4.944E-11 5.028E-11 5.326E-11 5.525E-11 5.681E-11 5.807E-11
    4    4   1    9.498E-13 1.129E-12 1.315E-12 1.358E-12 1.414E-12 1.489E-12 1.560E-12 1.621E-12 1.671E-12 1.711E-12 1.745E-12 1.773E-12 1.797E-12 1.893E-12 1.994E-12 2.246E-12 2.528E-12 2.803E-12 3.060E-12 3.298E-12 3.518E-12 3.722E-12 3.914E-12 4.729E-12 5.370E-12 5.876E-12 6.268E-12
    5    4   2    2.602E-12 2.885E-12 3.182E-12 3.254E-12 3.517E-12 3.972E-12 4.555E-12 5.230E-12 5.971E-12 6.763E-12 7.591E-12 8.444E-12 9.314E-12 1.371E-11 1.789E-11 2.517E-11 3.105E-11 3.586E-11 3.985E-11 4.325E-11 4.619E-11 4.879E-11 5.112E-11 6.031E-11 6.732E-11 7.317E-11 7.813E-11
    6    4   3    1.383E-11 1.630E-11 1.850E-11 1.868E-11 1.913E-11 2.014E-11 2.136E-11 2.265E-11 2.396E-11 2.525E-11 2.650E-11 2.773E-11 2.891E-11 3.423E-11 3.866E-11 4.543E-11 5.015E-11 5.346E-11 5.581E-11 5.750E-11 5.876E-11 5.974E-11 6.053E-11 6.344E-11 6.611E-11 6.886E-11 7.155E-11
    7    5   1    3.627E-13 4.225E-13 4.790E-13 4.952E-13 5.395E-13 6.089E-13 6.905E-13 7.757E-13 8.600E-13 9.414E-13 1.019E-12 1.092E-12 1.162E-12 1.451E-12 1.665E-12 1.965E-12 2.182E-12 2.367E-12 2.541E-12 2.711E-12 2.882E-12 3.052E-12 3.222E-12 4.046E-12 4.790E-12 5.431E-12 5.963E-12
    8    5   2    4.752E-13 5.685E-13 6.563E-13 6.844E-13 7.533E-13 8.520E-13 9.663E-13 1.091E-12 1.224E-12 1.365E-12 1.513E-12 1.668E-12 1.829E-12 2.704E-12 3.646E-12 5.506E-12 7.179E-12 8.632E-12 9.893E-12 1.100E-11 1.197E-11 1.284E-11 1.362E-11 1.660E-11 1.868E-11 2.027E-11 2.155E-11
    9    5   3    2.314E-12 2.672E-12 2.994E-12 3.069E-12 3.318E-12 3.740E-12 4.271E-12 4.876E-12 5.535E-12 6.236E-12 6.971E-12 7.734E-12 8.520E-12 1.264E-11 1.681E-11 2.466E-11 3.158E-11 3.760E-11 4.289E-11 4.756E-11 5.175E-11 5.552E-11 5.895E-11 7.257E-11 8.256E-11 9.049E-11 9.695E-11
   10    5   4    1.337E-11 1.597E-11 1.820E-11 1.849E-11 1.920E-11 2.052E-11 2.209E-11 2.370E-11 2.528E-11 2.679E-11 2.823E-11 2.960E-11 3.090E-11 3.648E-11 4.090E-11 4.751E-11 5.224E-11 5.585E-11 5.878E-11 6.126E-11 6.344E-11 6.540E-11 6.719E-11 7.447E-11 8.003E-11 8.451E-11 8.812E-11
   11    6   1    1.235E-13 1.397E-13 1.550E-13 1.603E-13 1.793E-13 2.101E-13 2.490E-13 2.940E-13 3.439E-13 3.980E-13 4.556E-13 5.161E-13 5.789E-13 9.136E-13 1.254E-12 1.880E-12 2.404E-12 2.836E-12 3.194E-12 3.497E-12 3.755E-12 3.979E-12 4.177E-12 4.924E-12 5.472E-12 5.935E-12 6.343E-12
   12    6   2    2.320E-13 2.707E-13 3.068E-13 3.189E-13 3.550E-13 4.099E-13 4.757E-13 5.476E-13 6.230E-13 7.007E-13 7.796E-13 8.592E-13 9.394E-13 1.345E-12 1.759E-12 2.607E-12 3.463E-12 4.303E-12 5.113E-12 5.888E-12 6.626E-12 7.329E-12 7.998E-12 1.090E-11 1.323E-11 1.510E-11 1.659E-11
1
Are you interested in linear only, or might you also use a quadratic? I ask as the data does not visually appear to be on a straight line, and a quadratic should give a much better fit. - James Phillips
A quadratic should also work for what I'm doing. I'm just not sure how to do this. - Cosmoman
According to the numpy documentation at docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html you would replace every call to np.polyfit(Delta, C_Values, 1) with np.polyfit(Delta, C_Values, 2) to fit to a polynomial of degree 2. There would now be three parameter values to print as well. - James Phillips

1 Answers

1
votes

You can create several subplots by using plt.subplots(). You can then iterate over the subplots and plot your data into the respective subplot as shown below.

import numpy as np; np.random.seed(1) 
import matplotlib.pyplot as plt

y = np.random.normal(size=(36,12))
x = np.random.rayleigh(size=(36,12))
x0 = np.linspace(0,3)

fig, axes = plt.subplots(ncols=3, nrows=4, sharex=True, sharey=True, figsize=(7,7))

for i, ax in enumerate(axes.flatten()):
    ax.scatter(x[:,i], y[:,i], s=10)
    m, b = np.polyfit(x[:,i], y[:,i], 1)
    ax.plot(x0, m*x0+b, lw=1., label="y={:.2f}x+{:.2f}".format(m,b))
    ax.legend()

plt.tight_layout()    
plt.show()

enter image description here