我试图绘制数据前1000点的运行平均值(每月太阳黑子数)。我应该使用Y_k = t / 2r(从m = -r到Y_k + m的r之和),其中r = 5,y_k是太阳黑子的数量。到目前为止我有这个。我已经导入了所有内置函数
if part == 'C':
y = data[:,1]
x = data[:,0]
x_2 = x[:1000]
y_Average_Sunspot = np.empty(1000,float)
y_Avg = y[:1000]
x_Avg = x[:1000]
y_n5 = (y_Avg)-5
y_n4 = (y_Avg)-4
y_n3 = (y_Avg)-3
y_n2 = (y_Avg)-2
y_n1 = (y_Avg)-1
y_1 = (y_Avg)+1
y_2 = (y_Avg)+2
y_3 = (y_Avg)+3
y_4 = (y_Avg)+4
y_5 = (y_Avg)+5
y_Average = (y_5)+(y_4)+(y_3)+(y_2)+(y_1)+(y_n1)+(y_n2)+(y_n3)+(y_n4)+(y_n5)+(y_Avg)
y_Average_Sunspots = y_Average/10
plt.plot(x_2,y,label='First Thousand Months',color='y')
plt.plot(x_Avg, y_Average_Sunspot,label='Running Average', color='k')
plt.xlim(0,1000)
plt.ylim(0,1000)
plt.xlabel('Time (months)')
plt.ylabel('Number of Sunspots')
plt.legend(loc=9)
plt.show()
问题是当我导入它时说x和y需要具有相同的第一维。我已经检查了这个程序中每个数组的形状和大小,它们都是一样的。
完整错误追溯
ValueError Traceback(最近一次调用最后一次) in()
----> 1 ps.plot_sunspots(' C')
plot_sunspots中的/home/loberhel/ASTR260/HW2/plot_sunspot.pyc(部分) 111 112 - > 113 plt.plot(x_2,y,label =' First Thousand Months',color =' y')114 plt.plot(x_Avg, y_Average_Sunspot,label='Running Average',
颜色=' K&#39) 115 plt.xlim(0,1000)
/usr/lib64/python2.7/site-packages/matplotlib/pyplot.pyc in plot(* args,
** kwargs) 2985 ax.hold(hold)
2986尝试:
- > 2987 ret = ax.plot(* args,** kwargs)
2988 draw_if_interactive()
最后2989:/usr/lib64/python2.7/site-packages/matplotlib/axes.pyc in plot(self,* args,
** kwargs)
4135行= [] 4136 - > 4137 for self in__get_lines(* args,** kwargs):
4138 self.add_line(line)
4139 lines.append(line)
/usr/lib64/python2.7/site-packages/matplotlib/axes.pyc_grab_next_args(self,* args,** kwargs)
315 return
316 if len(remaining) <= 3:
- &GT; 317 for seg in self._plot_args(剩余,kwargs):
318 yield seg
319 return
_plot_args中的236 if x.shape[0] != y.shape[0]:
- &GT; 237引发ValueError(&#34; x和y必须具有相同的第一维&#34;)
238 if x.ndim > 2 or y.ndim > 2:
239 raise ValueError("x and y can be no greater than 2-D")
ValueError:x和y必须具有相同的第一维