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?

推荐答案

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()

Python相关问答推荐

对于一个给定的数字,找出一个整数的最小和最大可能的和

Vectorize多个头寸的止盈/止盈回溯测试pythonpandas

2D空间中的反旋算法

Polars:用氨纶的其他部分替换氨纶的部分

如何禁用FastAPI应用程序的Swagger UI autodoc中的application/json?

在matplotlib中删除子图之间的间隙_mosaic

为什么常规操作不以其就地对应操作为基础?

OpenGL仅渲染第二个三角形,第一个三角形不可见

为什么我的sundaram筛这么低效

为什么t sns.barplot图例不显示所有值?'

提取数组每行的非零元素

如何设置nan值为numpy数组多条件

来自Airflow Connection的额外参数

如何在Quarto中的标题页之前创建序言页

在Django REST框架中定义的URL获得404分

PyTorch变压器编码器中的填充掩码问题

如何在polars group_by中将多个行分组到列表中

在被零整除的情况下,Python不遵循IEEE-754吗?

从pandas框架中删除重复的子框架

matplotlib散点图与NaNs和cmap colored颜色 矩阵