我是python的新手,所以如果这太琐碎,我很抱歉。
这是文本文件前两行的示例。
ra dec major_axis minor_axis position_angle
149.20562 2.29594 0.00418 0.00310 83.40
文件的每一行都有5个参数,用于绘制一个椭圆。前两列是中心。接下来的3列分别是长轴,短轴和位置角。此文件是包含许多行的大型目录的一部分。我想在一个图中绘制所有这些椭圆。
这是我尝试过的。
import matplotlib.pyplot as plt
import numpy as np
import astropy.io.ascii as asciitable
from matplotlib.patches import Ellipse
path=/users/vishnu/Desktop/
fw=open(path + 'data_plot.txt', 'r')
table = asciitable.read(path+ "data_plot.txt")
ra_degrees=[table['ra']]
dec_degrees=[table['dec']]
major_axis_deg=[table['major_axis']]
minor_axis_deg=[table['minor_axis']]
position_angle_deg=[table['position_angle']]
for ra, dec, w, h, angle in zip(ra_degrees,
dec_degrees,major_axis_deg, minor_axis_deg, position_angle_deg):
ellipse = Ellipse(xy=(ra, dec), width=w, height=h, angle=angle)
ax.add_patch(ellipse)
plt.axis('scaled')
plt.show()
fw.close()
这是错误日志。
runfile('/users/vishnu/.spyder2-py3/radio_sources.py', wdir='/users/vishnu/.spyder2-py3')
Traceback (most recent call last):
File "<ipython-input-299-a0011c0326f5>", line 1, in <module>
runfile('/users/vishnu/.spyder2-py3/radio_sources.py', wdir='/users/vishnu/.spyder2-py3')
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/users/vishnu/.spyder2-py3/radio_sources.py", line 63, in <module>
ax.add_patch(ellipse)
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 1783, in add_patch
self._update_patch_limits(p)
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 1803, in _update_patch_limits
xys = patch.get_patch_transform().transform(vertices)
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/matplotlib/patches.py", line 1409, in get_patch_transform
self._recompute_transform()
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/matplotlib/patches.py", line 1398, in _recompute_transform
.scale(width * 0.5, height * 0.5) \
File "/users/vishnu/anaconda3/lib/python3.5/site-packages/matplotlib/transforms.py", line 1965, in scale
np.float_)
ValueError: setting an array element with a sequence.
如果有更聪明的方法可以在不需要创建数组的情况下告诉我。
答案 0 :(得分:0)
好像你需要使用PatchCollection,请参阅this example。试试这样的事情
plt.figure()
ax = plt.gca()
ellipses = []
plt.xlim([0,100])
plt.ylim([0,100])
for ra, dec, w ... zip(...):
ellipse = Ellipse(xy=(ra, dec), width=w, height=h, angle=angle)
ellipses.append(ellipse)
p = PatchCollection(ellipses)
ax.add_collection(p)
plt.show()
答案 1 :(得分:0)
我猜测问题是你在ra_degrees上有太多的嵌套级别,等等。
也就是说,如果您打印表[&#39; ra&#39;],您可能会发现它已经是一个数组。当您将其括在方括号中时,[table [&#39; ra&#39;]]将成为长度为1的列表。 zip函数一次从每个序列中获取一个项目,因此在for循环中,ra将被分配表[&#39; ra&#39;]第一次也是唯一一次循环。
另一个问题是您要打开文件两次。您将文件名传递给asciitable.read,并且实际上从未使用过fw。
以下代码可能会更好。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import astropy.io.ascii as asciitable
path=/users/vishnu/Desktop/
table = asciitable.read(path + "data_plot.txt")
ra_degrees = table['ra']
dec_degrees = table['dec']
major_axis_deg = table['major_axis']
minor_axis_deg = table['minor_axis']
position_angle_deg = table['position_angle']
for ra, dec, w, h, angle in zip(ra_degrees, dec_degrees,
major_axis_deg, minor_axis_deg, position_angle_deg):
ellipse = Ellipse(xy=(ra, dec), width=w, height=h, angle=angle)
ax.add_patch(ellipse)
plt.axis('scaled')
plt.show()
如果asciitable适用于打开文件而不是文件名,请改用以下内容:
with open(path + "data_plot.txt") as fw:
table = asciitable.read(fw)
这确保即使asciitable.read中存在导致它引发异常的错误,文件也会关闭。但是,鉴于上述错误,您似乎已经阅读了数据。