pyplot中辅助x轴上的刻度错位

时间:2014-09-02 02:20:31

标签: python matplotlib

我正在尝试绘制一个时间序列,其标签对应于格鲁吉亚天128和158之间的底部和当地时间(GMT-7)的GMT时间。我使用twiny()来创建次要x-轴,但不知何故,它上面的标签放置不正确。我该如何解决这个问题?

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

fig = plt.figure(figsize=(20,6))
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()

dayTickLocations=range(128*24,158*24,24)
dayTickLabels=range(128,158)

dayTickLocations_loc=range((128*24)-7,(158*24)-7,24)
dayTickLabels_loc=range(128,158)

ax1.set_xlim(128*24,158*24)
ax1.set_xlabel('Julian Days (GMT Time)')
ax1.set_xticks(dayTickLocations)
ax1.set_xticklabels(dayTickLabels)
ax1.grid(True)

ax2.grid(True)
ax2.set_xlabel('Julian Days (Local Time)')
ax2.set_xlim(128*24,158*24)
ax2.set_xticks(dayTickLocations_loc)
ax2.set_xticklabels(dayTickLabels_loc)

plt.show()

enter image description here

1 个答案:

答案 0 :(得分:0)

更改' ax2'中命令的顺序块以某种方式解决了问题,虽然我不明白如何。

ax2.set_xlabel('Julian Days (Local Time)')
ax2.set_xticks(dayTickLocations_loc)
ax2.set_xticklabels(dayTickLabels_loc)
ax2.grid(True)
ax2.set_xlim(128*24,158*24)