Home > Blockchain >  How can I add many datasets to a single plot in python?
How can I add many datasets to a single plot in python?

Time:08-06

I am wanting to plot many datasets on one plot in python. Currently, I have hardcoded 20 of the datasets for the sake of testing my plot. Here is the shortened code below:

import numpy as np
import matplotlib as plt

N_steps = 1000
N_plot = 20
xorb_list = np.empty((N_plot, N_steps))
yorb_list = np.empty((N_plot, N_steps))
xobs = np.empty((N_plot, N_steps))
yobs = np.empty((N_plot, N_steps))
zobs = np.empty((N_plot, N_steps))


for i in range(N_plot):
   for k in range(N_steps):
        xorb_list[i][k] = r[k] * cm_to_arcsec * cos_theta[k]
        yorb_list[i][k] = r[k] * cm_to_arcsec * sin_theta[k]
        xobs[i][k] = r[k] * (cos_lon * cos_w_plus_nu[k] - sin_lon * sin_w_plus_nu[k] * cos_i)
        yobs[i][k] = r[k] * (sin_lon * cos_w_plus_nu[k]   cos_lon * sin_w_plus_nu[k] * cos_i)
        zobs[i][k] = r[k] * sin_i * sin_w_plus_nu[k]

# Plotting the observed orbits

fig1 = plt.figure(1)
ax1 = fig1.add_subplot()
ax1.set_aspect(1)
ax1.set_title("Observed Stellar Orbits")

ax1.plot(xobs[0], yobs[0])
ax1.plot(xobs[1], yobs[1])
ax1.plot(xobs[2], yobs[2])
ax1.plot(xobs[3], yobs[3])
ax1.plot(xobs[4], yobs[4])
ax1.plot(xobs[5], yobs[5])
ax1.plot(xobs[6], yobs[6])
ax1.plot(xobs[7], yobs[7])
ax1.plot(xobs[8], yobs[8])
ax1.plot(xobs[9], yobs[9])
ax1.plot(xobs[10], yobs[10])
ax1.plot(xobs[11], yobs[11])
ax1.plot(xobs[12], yobs[12])
ax1.plot(xobs[13], yobs[13])
ax1.plot(xobs[14], yobs[14])
ax1.plot(xobs[15], yobs[15])
ax1.plot(xobs[16], yobs[16])
ax1.plot(xobs[17], yobs[17])
ax1.plot(xobs[18], yobs[18])
ax1.plot(xobs[19], yobs[19])

plt.show()

The total number of datasets will be around 100, which will not be fun to hardcode. Is there some way to generate these datasets for any arbitrary number of sets?

CodePudding user response:

You can loop through your dataframe by its length. Then, place plt.show() outside the for loop like:

for x in range(len(xobs)): 
    ax1.plot(xobs[x], yobs[x])
plt.show()
  • Related