I have seen many examples online of how to plot multiple results on a single set of axes but all of these follow a single set of instructions.
I am modularising my code, creating a set of global values, followed by a set of definitions and, finally, my main instructions; the reduced version being…
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import numpy as np
import math
SIGMA = 5.67051e-5
PI = math.pi
M_SUN = 1.989e33
L_SUN = 3.826e33
R_SUN = 6.9599e10
YEAR = 3.15570e7
sa_sun = 4 * PI * math.pow(R_SUN, 2)
t_sun = L_SUN / (sa_sun * SIGMA)
...SOME CODE HERE...
def CM_Graph(luminosity, radius):
l_star = luminosity / L_SUN
r_star = np.power(radius / R_SUN, 2)
t_star = np.power((t_sun * l_star) / r_star, 0.25)
fig, axs = plt.subplots()
axs.loglog(t_star[:], l_star[:])
axs.invert_xaxis()
axs.set_title(r'Colour Magnitude (CM) Diagram')
axs.set_xlabel(r'Surface Temperature (Kelvin) - ${(\frac{t_{\odot} \times L_{\star}}{R_{\odot}})}^\frac{1}{4}$')
axs.set_ylabel(r'Luminosity (Solar Units) - $\frac{L_{\star}}{L_{\odot}}$')
plt.show()
if __name__ == "__main__":
stellarData = readData("dummymain.lst")
CM_Graph(stellarData[:, 4],stellarData[:, 3])
This works perfectly to produce a single graph, where the data is taken from a single file (readData
reads in the data from the file).
However, I intend on using multiple files and want the data to plot on the single axes; with the axes adjusting to match the data as a whole.
CM_Graph
is going to be 1 of many graphs that the system will produce, hence the modularisation (and other reasons). I can see that I would create the subplots axs
outside of the definitions and parse this in, but what then? Should the plt.show
be in my main (thus, plotting all graphs). Do I call axs
as GLOBAL within the CM_Graph, and would subsequent plots overwrite the earlier ones?
Sorry, so many questions.
An aside; what is fig
for, on the line fig, axs = plt.subplots()
? It is never used and I cannot find it mentioned in the documentation.