【问题标题】:Overplot data with multiple x-axis in Python在 Python 中使用多个 x 轴覆盖数据
【发布时间】:2013-03-15 19:12:42
【问题描述】:

我正在尝试从包含计数率与时间的 .fits 文件中绘制一些数据。 我的目标是在同一个图中过度绘制不同事件的计数率,在三个不同的 x 轴上具有不同的时间。 由于我要绘制的事件是周期性的,因此我对正确时间的识别施加了一个条件,因此我只能从我需要的时间范围中提取计数率。 这是我的努力:

#!/usr/bin/env python

from scipy import *
from numpy import *
from pylab import *
from scipy import optimize
import pyfits, os, re, glob
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.ticker import FuncFormatter

rc('font',**{'family':'serif','serif':['Helvetica']})
rc('ps',usedistiller='xpdf')
rc('text', usetex=True)

#------------------------------------------------------

tmin=56200
tmax=56249

data=pyfits.open('http://heasarc.gsfc.nasa.gov/docs/swift/results/transients/weak/GX304-1.orbit.lc.fits')

time  = data[1].data.field(0)/86400. + data[1].header['MJDREFF'] + data[1].header['MJDREFI']
rate  = data[1].data.field(1)
error = data[1].data.field(2)
data.close()

cond= ((time > tmin-5) & (time < tmax)) | ((time + 132.5 > tmin) & (time + 132.5 < tmax-10)) | ((time + 265 > tmin) & (time + 265 < tmax-12))
time=time[cond]
rate=rate[cond]
error=error[cond]

fig, ax1 = plt.subplots()
newax2 = ax1.twiny()
newax3 = ax1.twiny()

# Make some room at the bottom
fig.subplots_adjust(bottom=0.30)

newax2.set_frame_on(True)
newax2.patch.set_visible(False)
newax2.xaxis.set_ticks_position('bottom')
newax2.xaxis.set_label_position('bottom')
newax2.spines['bottom'].set_position(('outward', 20))

newax3.set_frame_on(True)
newax3.patch.set_visible(False)
newax3.xaxis.set_ticks_position('bottom')
newax3.xaxis.set_label_position('bottom')
newax3.spines['bottom'].set_position(('outward', 40))

#..plot the current light curve
errorbar(time, rate, error, fmt='r.', capsize=0)
gca().xaxis.set_major_formatter(FormatStrFormatter('%5.1f'))

#..overplot a previous outburst
errorbar(time + 122.5, rate, error, fmt='g.', capsize=0)
errorbar(time + 255, rate, error, fmt='k.', capsize=0)

axis([tmin-10,tmax,-0.00,0.45])
xlabel('Time, MJD')
ylabel("BAT cts/s/cm$^2$")
savefig("sync.eps",orientation='portrait',papertype='a4',format='eps')
os.system('gv sync.eps')

我正在绘制三个事件,所以我需要三个 x 轴;但是,如果可能的话,我也想用相应的颜色在 x 轴上写下相应的时间。底部报告的时间是红色曲线的正确时间,即最近的事件。 请问有什么建议吗? 非常感谢。

【问题讨论】:

  • this question and answers 有帮助吗?
  • 感谢埃弗特的回复。但是,我也需要“重新定义”x 轴,以显示每个数据系列的正确时间。而且我认为这比仅仅改变轴上的颜色更棘手。无论如何,很快或之后我也会需要那个部分。
  • 顺便说一句,您是否有理由不让 matplotlib/pylab 为您显示图形,而是通过 gv 运行它?然后,您可以从 pylab 交互式图形窗口中保存文件。还是 pylab 在您的系统上配置不当导致您无法使用这种交互式方法?

标签: python


【解决方案1】:

如果我正确理解您的问题,问题是第一个和第二个 x 轴上的刻度不正确。 您应该简单地将 errorbar 函数绑定到正确的轴,然后 matplotlib 将为您进行缩放。像这样:

...
#..plot the current light curve
ax1.errorbar(time, rate, error, fmt='r.', capsize=0)
gca().xaxis.set_major_formatter(FormatStrFormatter('%5.1f'))

#..overplot a previous outburst
newax2.errorbar(time + 122.5, rate, error, fmt='g.', capsize=0)
newax3.errorbar(time + 255, rate, error, fmt='k.', capsize=0)
...

无论如何,我通常更喜欢 matplotlib 中更面向对象的方法,所以这是唯一的方法。

如果您不介意,我会将着色部分留给我评论中的参考。

更新

以下是一些更新的代码;也许这就是你要找的。 诀窍是移动 x 数据 (time),而只需将 x 限制(您正在查看的“窗口”)移动到您想要的位置。所以我将 122.5 和 255 偏移量(反向)分别应用于每个轴的 x 限制。

我还必须分别为每个轴设置格式化程序;如果我不设置格式化程序,你最终会得到一个偏移符号(0 .. 10 .. 20 .. 30 + 56190)。像你一样使用全局格式化程序,(我认为)只能在ax1 上工作。 请注意,我只在全局范围内设置 y 限制,注释掉 axis 命令。

...
##..plot the current light curve
ax1.errorbar(time, rate, error, fmt='r.', capsize=0)
ax1.set_xlim(tmin-10,tmax)
ax1.xaxis.set_major_formatter(FormatStrFormatter('%5.1f'))
##..overplot a previous outburst
newax2.errorbar(time, rate, error, fmt='g.', capsize=0)
newax3.errorbar(time, rate, error, fmt='k.', capsize=0)
newax2.set_xlim(tmin-10-122.5,tmax-122.5)
newax2.xaxis.set_major_formatter(FormatStrFormatter('%5.1f'))
newax3.set_xlim(tmin-10-255,tmax-255)
newax3.xaxis.set_major_formatter(FormatStrFormatter('%5.1f'))
ylim(0,0.45)
#axis([tmin-10,tmax,-0.00,0.45])
...

这会导致 这是您的原始脚本生成的,但希望有一组正确的 x 轴(除了着色)。

【讨论】:

  • 再次感谢埃弗特。问题是,按照您的建议,我没有获得“过度绘图”,而是“多情节”,就像这里显示的那样:tinypic.com/view.php?pic=15zlcht&s=6
  • 好吧,数据被重叠绘制成一张图;那你所说的过度绘制还有什么意思?就我的定义而言,多图具有多个图形,而您的图形没有。
  • 如果您运行我编写的原始代码,您可以了解我所需要的内容以及我通过“multi-plot”获得的内容
  • @Py-ser:我已经更新了我的答案;让我知道这是否是您要找的。​​span>
  • 是的,Evert,这就是我需要的。因此,不必直接输入不同的数据集,只需将窗口移到取x轴数据的位置即可。好的!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2013-04-30
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 2015-10-10
相关资源
最近更新 更多