matplotlib 如何在一个图中对齐两个x轴的值

cigdeys3  于 6个月前  发布在  其他
关注(0)|答案(2)|浏览(53)

我尝试在matplotlib中使用twiny()来绘制一条带有两个x轴的曲线,该曲线来自一个由以下数据块组成的XML文件:

<data>
<meas>
  <utc>2018-11-10T22:27:06.500003</utc>
  <ra_j2000>23.9722686269</ra_j2000>
  <dec_j2000>-1.23845121893</dec_j2000>
  <mag>9.96074403533</mag>
</meas>

<meas>
  <utc>2018-11-10T22:27:54.500002</utc>
  <ra_j2000>23.9930913364</ra_j2000>
  <dec_j2000>-1.03788334773</dec_j2000>
  <mag>11.356437889</mag>
</meas>

<meas>
  <utc>2018-11-10T22:38:36.500002</utc>
  <ra_j2000>0.267638646848</ra_j2000>
  <dec_j2000>1.56055091433</dec_j2000>
  <mag>11.1642458641</mag>
</meas>

<meas>
  <utc>2018-11-10T22:46:18.500000</utc>
  <ra_j2000>0.462353662364</ra_j2000>
  <dec_j2000>3.34334963425</dec_j2000>
  <mag>11.1082534741</mag>
</meas>

<meas>
  <utc>2018-11-10T22:57:18.500001</utc>
  <ra_j2000>0.740393528722</ra_j2000>
  <dec_j2000>5.78641590694</dec_j2000>
  <mag>11.0688955214</mag>
</meas>

<meas>
  <utc>2018-11-10T23:03:06.499995</utc>
  <ra_j2000>0.888541738338</ra_j2000>
  <dec_j2000>7.03265231497</dec_j2000>
  <mag>10.2358937709</mag>
</meas>

<meas>
  <utc>2018-11-10T23:05:42.500002</utc>
  <ra_j2000>0.955591973177</ra_j2000>
  <dec_j2000>7.5832430461</dec_j2000>
  <mag>10.86206725</mag>
</meas>

<meas>
  <utc>2018-11-10T23:06:48.499999</utc>
  <ra_j2000>0.984093767077</ra_j2000>
  <dec_j2000>7.81466175077</dec_j2000>
  <mag>10.3466108708</mag>
</meas>
</data>

字符串
我的问题是我在这些x轴上得到了不对齐的值。下面是我的Python脚本:

import math
import xml.etree.ElementTree as ET
from astropy.time import Time
from astropy.coordinates import get_sun
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib import dates

tree = ET.parse('20181110_10241.xml')
root = tree.getroot()

x_ut = []
x_phi = []
y_brightness = []

def convert_time(obs_time):
    obs_time = str(obs_time)
    d, t = obs_time.split('T')
    year, month, day = map(int, d.split('-'))
    hour, minute, second = t.split(':')
    return datetime(year, month, day, int(hour), int(minute)) + \
        timedelta(seconds=float(second))

def get_sun_coords(obs_time):
    sun_coords = get_sun(obs_time)
    sun_ra = sun_coords.ra.degree
    sun_dec = sun_coords.dec.degree
    return sun_ra, sun_dec

def get_phase_angle(sun_ra, sun_dec, target_ra, target_dec):
    phase_angle = math.degrees(math.acos(-math.sin(math.radians(sun_dec))*math.sin(math.radians(target_dec)) - math.cos(math.radians(sun_dec))*math.cos(math.radians(target_dec))*math.cos(math.radians(sun_ra-target_ra))))
    return phase_angle

for meas in root.findall('meas'):
    obs_time = Time(meas.find('utc').text, format='isot', scale='utc')
    target_ra = float(meas.find('ra_j2000').text)*15
    target_dec = float(meas.find('dec_j2000').text)
    mag = float(meas.find('mag').text)

    sun_ra, sun_dec = get_sun_coords(obs_time)
    phase_angle = get_phase_angle(sun_ra, sun_dec, target_ra, target_dec)

    obs_time = convert_time(obs_time)
    x_ut.append(obs_time)
    x_phi.append(phase_angle)
    y_brightness.append(mag)

fig, ax1 = plt.subplots()

ax1.plot(x_ut, y_brightness, marker='o', label='apparent brightness')
ax1.set_xlim(x_ut[0],x_ut[-1])
ax1.xaxis.set_major_locator(dates.MinuteLocator(interval=1))
ax1.xaxis.set_major_formatter(dates.DateFormatter('%H:%M'))
ax1.tick_params(axis='x', rotation=45)
ax1.minorticks_on()
ax1.legend()
ax1.grid()
ax1.set_xlabel('time [h:m, UT]')
ax1.set_ylabel('apparent brightness [mag, CR]')

ax2 = ax1.twiny()
ax2.plot(x_phi,y_brightness, marker='^', color='red')
ax2.set_xlim(x_phi[0],x_phi[-1])
ax2.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax2.minorticks_on()
ax2.set_xlabel('phase angle (phi) [deg]')

plt.gca().invert_yaxis()
plt.tight_layout(pad=0)
plt.show()


这将产生以下图:
x1c 0d1x的数据
我打算稍后隐藏红色曲线(通过使用visibility=False),在这里我绘制它只是为了查看x轴值的正确对齐,也就是说,两条曲线必须(!)重叠,因为相位角(x_phi)值取决于相应的时间戳(x_ut)值,但正如您可以清楚地看到的那样,只有开头和结尾正确对齐,但中间的大部分数据都没有对齐(相位曲线向右移动)。
我做错了什么?
最初,我假设相位角(x_phi)随时间非线性变化,因此两条曲线的set_xlim()以不同的方式拉伸它们,但这不是真的,我绘制了x_phix_ut,并且有明显的线性变化:



提前感谢您的任何帮助!

**编辑:**非线性已经被tel在下面的回答中证明了。因此,我稍微改变了我的问题。

如果我从两个子图ax1ax2中删除set_xlim(),那么:
1)上面的x轴自动反转,从最小值开始,尽管给出值的列表x_phi从最大值开始--我如何避免这种反转而不使用invert_axis()?(在不同的情况下,我在x_phi列表中总是只有增加或减少的值)
2)总共有3个列表:x_utx_phiy_brightness;我只需要实际绘制y_brightnessx_ut的曲线,另外还需要将x_phi(带有ticker.MultipleLocator(1))的值与x_ut中相应的时刻值对齐--我该怎么做呢?
我的问题类似于这个:How do I align gridlines for two y-axis scales using Matplotlib?,但是在我的例子中,x轴上部的刻度之间没有线性间距,所以我不能使用该解决方案。
另外,这个问题也涉及到一个类似的问题:trouble aligning ticks for matplotlib twinx axes但是我不知道如何表达两个x轴之间的关系,因为数据类型非常不同:datetime和float。它们之间的唯一关系是一对一的,也就是说,来自x_ut的第一个值与来自x_phi的第一个值相关,第二个值与第二个值相关,等等;这种关系是非线性的。

**编辑2:**我之前编辑中的数字1)现在已经解决了。对于剩下的问题,看起来我必须使用register_scale()来重新缩放次x轴相对于主x轴的比例。要做到这一点,我还必须定义matplotlib.scale.ScaleBase的子类。到目前为止,我只找到了两个复杂的(对我来说)示例:

https://matplotlib.org/examples/api/custom_scale_example.html
https://stackoverrun.com/es/q/8578801(西班牙语,但代码中有英文注解)
我不确定我是否能够自己实现这一点,所以我仍然寻求任何帮助。

wh6knrhe

wh6knrhe1#

好耶!我已经成功地得到了所寻求的结果,而无需定义新的scale类!以下是从问题中添加/修改的脚本中的相关代码部分(变量step稍后将从用户命令行输入中读取,或者我可能会找到另一种自动设置滴答频率的方法):

x_ut = []
x_phi = []
x_phi_ticks = []
x_phi_ticklabels = []
y_brightness = []

# populate lists for the phase angle ticks and labels

i = 0
step = 15
while i <= (len(x_ut)-step):
    x_phi_ticks.append(x_ut[i])
    x_phi_ticklabels.append(x_phi[i])
    i += step
x_phi_ticks.append(x_ut[-1])
x_phi_ticklabels.append(x_phi[-1])

# plot'em all

fig, ax1 = plt.subplots()

ax1.plot(x_ut, y_brightness, marker='o', label='apparent brightness')
ax1.xaxis.set_major_locator(dates.MinuteLocator(interval=1))
ax1.xaxis.set_major_formatter(dates.DateFormatter('%H:%M'))
ax1.tick_params(axis='x', rotation=45)
ax1.minorticks_on()
ax1.legend()
ax1.grid(which='major', linestyle='-', color='#000000')
ax1.grid(which='minor', linestyle='--')
ax1.set_xlabel('time [h:m, UT]')
ax1.set_ylabel('apparent brightness [mag, CR]')

ax2 = ax1.twiny()
ax2.set_xlim(ax1.get_xlim())
ax2.set_xticks(x_phi_ticks)
ax2.set_xticklabels(x_phi_ticklabels)
ax2.set_xlabel('phase angle (phi) [deg]')

plt.gca().invert_yaxis()
plt.tight_layout(pad=0)
plt.show()

字符串
x1c 0d1x的数据

ghhkc1vu

ghhkc1vu2#

您的绘图程序看起来是正确的。相反,看起来问题在于时间和相位角之间的关系存在 *(非常小)偏离线性。您可以通过在时间与Angular 图上覆盖一条黑色直线来查看(单击下面以在单独的窗口中打开并放大以清楚地看到它):


的数据
这里有一个放大,突出显示的偏差:



让两个x轴对齐的唯一方法是:

  • 操纵数据。
  • 操纵第二个x轴的刻度使其不一致,使图上49和48度之间的实际距离与图上45和44度之间的实际距离不同,依此类推。

这两种方法都是一个坏主意™,你不应该做任何一个。你需要按原样绘制数据,而不是试图用聪明的技巧来隐藏任何不一致的地方。否则会被认为是学术上的不诚实。️
你唯一的选择是找出时间和Angular 不像你期望的那样匹配的原因。这可能是你的代码中的问题,也许是你在get_phase_angle中使用的公式中的问题。或者它可能是你的底层数据集中的问题,例如,如果度测量中有一些噪音。或者它可能只是,在现实中,时间和Angular 并不具有线性关系。毕竟,地球不是完美的圆。

相关问题