python - Matplotlib——双胞胎 : how to align values of two x-axes in one plot?

标签 python matplotlib alignment axes

我尝试在 matplotlib 中使用 twiny() 从包含这些数据 block 的 XML 文件中绘制具有两个 x 轴的曲线:

<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()

生成以下图:

enter image description here

我打算稍后隐藏红色曲线(通过使用 visibility=False),在这里我绘制它只是为了查看 x 轴值的正确对齐,即两条曲线必须( !)事实上重叠,因为相位角(x_phi)值取决于相应的时间戳(x_ut)值,但正如您可以清楚地看到的,只有开始和末端对齐正确,但中间的大部分数据未对齐(相位曲线向右移动)。

我做错了什么?

最初,我认为相位角 (x_phi) 随时间非线性变化,因此两条曲线的 set_xlim() 拉伸(stretch)它们的方式不同,但是这事实并非如此,我已经针对 x_ut 绘制了 x_phi ,并且存在明显的线性变化:

enter image description here

感谢您提前提供的任何帮助!

编辑:非线性已由电话在下面的答案中证明。因此,我稍微改变了我的问题。

如果我从子图 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 (西类牙语,但代码内有英文注释)

我不确定我是否能够自己实现这一点,所以我仍然寻求任何帮助。

最佳答案

耶!我已经成功地获得了所寻求的结果,而无需定义新的规模类别!以下是问题脚本中添加/修改的相关代码部分(变量 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()

enter image description here

关于python - Matplotlib——双胞胎 : how to align values of two x-axes in one plot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53458959/

相关文章:

python - urllib2 和 BeautifulSoup 没有提取完整的网页

ios - SwiftUI:将 View 与 VStack 中另一个 View 的前缘和后缘对齐

html - 如何在 Phalcon 中创建水平表单

html - 为什么我的元素不能水平显示?

python - 如何在 Matplotlib 中手动指定 bins?

python - 对键也是字典的字典键进行排序

Python:查明另一个应用程序是否打开了文件的有效方法

python - Django render_to_response 渲染?

python - Matplotlib 两个图表上的相同直方图

python - 在 Python for 循环中创建多个绘图