我想要在一块地块上绘制许多数据集.目前,为了测试我的曲线图,我已经对20个数据集进行了硬编码.以下是简短的代码:

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

数据集的总数将在100左右,这对于硬编码来说将不是一件有趣的事情.有没有某种方法可以为任意数量的集合生成这些数据集?

推荐答案

您可以根据数据帧的长度对其进行循环.然后,将plt.show()放在for循环之外,如下所示:

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

Python相关问答推荐

Pystata:从Python并行运行stata实例

Django mysql图标不适用于小 case

有症状地 destruct 了Python中的regex?

如何让Flask 中的请求标签发挥作用

使用groupby Pandas的一些操作

如何获得每个组的时间戳差异?

判断solve_ivp中的事件

Gunicorn无法启动Flask应用,因为无法将应用解析为属性名或函数调用.'"'' "

Cython无法识别Numpy类型

pandas:在操作pandora之后将pandora列转换为int

从源代码显示不同的输出(机器学习)(Python)

Django Table—如果项目是唯一的,则单行

Python OPCUA,modbus通信代码运行3小时后出现RuntimeError

高效生成累积式三角矩阵

修改.pdb文件中的值并另存为新的

从列表中分离数据的最佳方式

是否将Pandas 数据帧标题/标题以纯文本格式转换为字符串输出?

具有不同坐标的tkinter canvs.cocords()和canvs.moveto()

Scipy.linprog的可行性有问题吗?(A_ub@x0<;=b_ub).all()为True-但是-linprog(np.zeros_like(X0),A_ub=A_ub,b_ub=b_ub)不可行

pyspark where子句可以在不存在的列上工作