【问题标题】:How do you loop through an array, perform calculations on the arrays and plot the results?如何循环遍历数组、对数组执行计算并绘制结果?
【发布时间】:2013-07-19 02:16:55
【问题描述】:

我对编程很陌生,希望对我的程序提供一些帮助。我正在尝试通过 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()

【问题讨论】:

  • 大概radec长度不一样。
  • 它们的长度都是 754。
  • 好的,已经解决了,但是现在它说“TypeError:'int'类型的对象没有len()”,即使我打印它时也想到了ra,dec和dist的长度相同为753

标签: python arrays loops plot


【解决方案1】:

我不懂语言,但通常索引越界是指起始索引和结束索引不正确。从零开始直到长度而不是长度 1 等。特别是在有人发布知道该语言的答案之前,可能值得一看。希望我能帮上忙

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多