如何遍历数组,对数组执行计算并绘制结果?

时间:2013-07-19 02:16:55

标签: python arrays loops plot

我对编程非常陌生,并希望对我的程序提供一些帮助。我试图通过for循环读取数组,对它们进行一些计算并将结果绘制在3D图形上,但它给了我错误:

IndexError: index 753 is out of bounds for axis 0 with size 753

以下是代码:

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt
import numpy as np
import math
from itertools import product, combinations
from numpy import *
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("equal")

ra_day = loadtxt("RA Days.txt")  
ra_minute = loadtxt("RA Minutes.txt")
ra_second = loadtxt("RA Seconds.txt")
ra = ra_day + (ra_minute/60) + (ra_second/3600)  

dec_day = loadtxt("DEC Days.txt")  
dec_minute = loadtxt("DEC Minutes.txt")
dec_second = loadtxt("DEC Seconds.txt")
dec = dec_day + (dec_minute/60) + (dec_second/3600)  

dist = loadtxt("Distance.txt") 

for i in range(len(ra)):
    x = math.cos(ra[i]) * (dist[i] * math.cos(dec[i]))
    y = math.sin(ra[i]) * (dist[i] * math.cos(dec[i]))
    z = dist * math.sin(dec[i])

ax.scatter([0],[0],[0],color="b",s=100)
ax.scatter([x],[y],[z],color="k",s=100)

plt.show()

1 个答案:

答案 0 :(得分:0)

我不知道语言,但通常一个索引超出界限的是起始和结束索引不正确。从零开始直到长度而不是长度为1等。特别值得一看的是,直到有人发布了解语言的答案。希望我可以帮助