Astropy / SkyCoord:对于大于180度的角度,如何使x轴显示完整的度数范围,而不显示负数?

问题描述

我有以下代码来绘制几对银河座标,但是对于x轴,希望x轴显示完整的度数范围,而不是显示大于180度的角度的负数。建议?我只是在忽略SkyCoord参数吗?

工作示例:

%matplotlib inline

import matplotlib.pyplot as plt
import pandas as pd
from astropy import units as u
from astropy.coordinates import SkyCoord

plt.rcParams['figure.figsize'] = [8,8] # [width,height]

df = pd.read_csv('sight_lines_coord.txt',delimiter='\s+',usecols=['l','b'])
stars = SkyCoord(df['l'],df['b'],frame='galactic',unit=u.deg)
plt.scatter(stars.l.wrap_at('180d'),stars.b)
plt.grid(True)

这是我的情节的样子。

enter image description here

数据框由两列组成:经度,纬度(例如325.0,-5.2)

我应该早些添加我的歉意。如果我使用wrap_at('360d')我会得到这个图,这不是我想要的。

enter image description here

在180包裹起来可以得到我想要的图片,我只需要我的xtick标签为非负数即可。

解决方法

pyplot.xticks是为此功能。调用时不带参数,它将返回一个元组(ticks,labels)ticks是所有当前x轴刻度的x坐标,labels是它们的标记。如果使用两个参数调用,它将设置刻度值。

有一个警告,就是有时从pyplot.xticks()获得的labels是空的,直到调用plt.show()之后(我不确定为什么)。这意味着您只应使用ticks来设置标签。

plt.figure()
x = np.arange(-30,30,1)
plt.plot(x,np.sin(x/10))
ticks,labels = plt.xticks()

for tick,label in zip(ticks,labels):
    tick_value = tick if (tick >= 0) else tick + 360
    label.set_text(str(tick_value))
    
plt.xticks(ticks,labels)

Plot generated by the above code,ticks ranging from 320 to 350,then jumping to 0 up to 40


反转X轴的外观类似于:

lower,upper = plt.xlim()
plt.xlim(upper,lower)
再次,不带任何参数调用它返回左边界,然后返回右边界。用两个参数调用,它设置左边界和右边界。通过翻转它们,我们可以反转轴。将其整合到上面的代码中可以得到:

The same plot as the above,but the x axis has been inverted


旧答案:

您使用Angle.wrap_at (docs here),我认为这是问题的根源。

stars.l.wrap_at('180d')会将数组中的所有角度强制为-180 stars.l.wrap_at('360d')。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...