python - 如何将多个数据集添加到 python 中的单个图中?

标签 python data-science physics

我想在 python 中的一个图上绘制多个数据集。目前,为了测试我的情节,我对 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 - 如何将多个数据集添加到 python 中的单个图中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73255821/

相关文章:

python - Django Rest Framework POST 更新(如果存在或创建)

python - 如何计算 Pandas 中每一列的每日平均值?

machine-learning - 如何将集群标签列添加回原始数据帧 - python,用于监督学习

audio - 如何在3D游戏中模拟声音物理?

python - Pygame,按轴旋转图像

python - 如果某个时间在某个时间范围内,则查找该时间并返回 Pandas 中的相应值?

python - 在ubuntu中升级python

python - 如何在scrapy spider中传递用户定义的参数

r - 具有异构变量的客户集的聚类

ios - Sprite 不受物理影响