Differences between revisions 1 and 5 (spanning 4 versions)
Revision 1 as of 2020-03-18 18:26:56
Size: 509
Comment:
Revision 5 as of 2020-06-08 12:08:34
Size: 3751
Comment:
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
=== 複数の時系列を縦に並べてplotする ===
[[https://gwpy.github.io/docs/stable/timeseries/plot.html#plotting-multiple-timeseries-together|ここ]]に書いてある、gwpy.plotのPlot()がオススメ。<<BR>>
注意点は、TimeSeriesのlistやTimeSeriesDictを引数にしてまとめて渡した場合は ''separate=True'' をしても分割されないという謎仕様。面倒だが1つ1つ手で並べる必要がある。

{{{#!python
from gwpy.plot import Plot
plot = Plot(l1hoft, h1hoft)
plot = Plot(l1hoft, h1hoft, separate=True, sharex=True)
plot.show()
}}}
{{https://gwpy.github.io/docs/stable/_images/plot-7.png}}

戻り値 (ここでは ''plot'')は、[[https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.figure.html|matplotlibのfigure]]だと思ってしまってよい。<<BR>>
なので、それぞれのy軸ラベルや全体のタイトルを付けるには以下のようにする。
{{{#!python
import matplotlib.pyplot as plt
axes = plot.get_axes()
ylabel = ['y1','y2']
for i, ax in enumerate(axes): ax.set_ylabel(ylabel[i])
plt.tight_layout()
plt.subplots_adjust(top=0.95)
plot.suptitle('title')
}}}

=== 時刻の基準を手で設定する ===
[[https://gwpy.github.io/docs/stable/timeseries/plot.html#plotting-multiple-timeseries-together|ここ]]を参照
{{{#!python
ax = plot.gca()
ax.set_epoch(1126259462) # GPS timeを入れる
}}}
Line 6: Line 36:


''.add_segments_bar()'' を使う([[https://gwpy.github.io/docs/latest/examples/miscellaneous/open-data-spectrogram.html|ここ]]を参照)。
''.add_segments_bar()'' を使う([[https://gwpy.github.io/docs/latest/examples/miscellaneous/open-data-spectrogram.html|ここ]]を参照)。<<BR>>
他にも ''.add_state_segments()'' や ''.add_dataqualityflag()'' といった似たものがある。詳細は[[https://gwpy.github.io/docs/latest/plotter/api.html#gwpy.plotter.Plot.add_line|Plotting API]]を参照。
Line 12: Line 41:
他にも ''.add_state_segments()'' や ''.add_dataqualityflag()'' といった似たものがある。詳細は[[https://gwpy.github.io/docs/latest/plotter/api.html#gwpy.plotter.Plot.add_line|Plotting API]]を参照。
=== スペクトルのパーセンタイル/σバンドを付ける ===
[[https://gwpy.github.io/docs/stable/examples/frequencyseries/percentiles.html|ここ]]を参照。手順としては、
 1. まずTimeSeriesから[[https://gwpy.github.io/docs/stable/api/gwpy.timeseries.TimeSeries.html#gwpy.timeseries.TimeSeries.spectrogram2|spectrogram2()]]を使ってnon-averaged power Spectrogramを作る。
 2. .percentile() でパーセンタイルのスペクトルを3つ作る。
 3. plot_mmm() で描画するとバンド付きでplotされる。

{{{#!python
from gwpy.timeseries import TimeSeries
from gwpy.plot import Plot

hoft = TimeSeries. ...
sg = hoft.spectrogram2(fftlength=4, overlap=2, window='hanning') ** (1/2.)
median = sg.percentile(50)
low = sg.percentile(5)
high = sg.percentile(95)

plot = Plot()
ax = plot.gca(xscale='log', xlim=(10, 1500), xlabel='Frequency [Hz]', yscale='log', ylim=(3e-24, 2e-20), ylabel=r'Strain noise [1/$\sqrt{\mathrm{Hz}}$]')
ax.plot_mmm(median, low, high, color='gwpy:ligo-hanford')
ax.set_title('LIGO-Hanford strain noise variation around GW170817', fontsize=16)
plot.show()
}}}
{{https://gwpy.github.io/docs/stable/_images/percentiles-4.png}}

パーセンタイルではなくmeanとσで表したい場合は、Spectrogramの2次元配列に対して時間軸(0軸)方向にmean, stdを取ればよい。
{{{#!python
mean = FrequencySeries(data=sg.mean(0), frequencies=SG.frequencies)
sigma = FrequencySeries(data=sg.std(0), frequencies=SG.frequencies)
}}}

== matplotlib色々 ==
[[https://qiita.com/simonritchie/items/da54ff0879ad8155f441|プロットの複雑なレイアウトはGridSpecが便利かも、という話]]

[[https://bunseki-train.com/axvspan-and-axhspan/|matplotlibで一定区間に背景色をつける方法]]

Plotの描画関連

複数の時系列を縦に並べてplotする

ここに書いてある、gwpy.plotのPlot()がオススメ。
注意点は、TimeSeriesのlistやTimeSeriesDictを引数にしてまとめて渡した場合は separate=True をしても分割されないという謎仕様。面倒だが1つ1つ手で並べる必要がある。

   1 from gwpy.plot import Plot
   2 plot = Plot(l1hoft, h1hoft)
   3 plot = Plot(l1hoft, h1hoft, separate=True, sharex=True)
   4 plot.show()

https://gwpy.github.io/docs/stable/_images/plot-7.png

戻り値 (ここでは plot)は、matplotlibのfigureだと思ってしまってよい。
なので、それぞれのy軸ラベルや全体のタイトルを付けるには以下のようにする。

   1 import matplotlib.pyplot as plt
   2 axes = plot.get_axes()
   3 ylabel = ['y1','y2']
   4 for i, ax in enumerate(axes): ax.set_ylabel(ylabel[i])
   5 plt.tight_layout()
   6 plt.subplots_adjust(top=0.95)
   7 plot.suptitle('title')

時刻の基準を手で設定する

ここを参照

   1 ax = plot.gca()
   2 ax.set_epoch(1126259462) # GPS timeを入れる

DQ Flagを添える

.add_segments_bar() を使う(ここを参照)。
他にも .add_state_segments().add_dataqualityflag() といった似たものがある。詳細はPlotting APIを参照。

https://gwpy.github.io/docs/latest/_images/open-data-spectrogram-5.png

スペクトルのパーセンタイル/σバンドを付ける

ここを参照。手順としては、

  1. まずTimeSeriesからspectrogram2()を使ってnon-averaged power Spectrogramを作る。

  2. .percentile() でパーセンタイルのスペクトルを3つ作る。
  3. plot_mmm() で描画するとバンド付きでplotされる。

   1 from gwpy.timeseries import TimeSeries
   2 from gwpy.plot import Plot
   3 
   4 hoft = TimeSeries. ...
   5 sg = hoft.spectrogram2(fftlength=4, overlap=2, window='hanning') ** (1/2.)
   6 median = sg.percentile(50)
   7 low = sg.percentile(5)
   8 high = sg.percentile(95)
   9 
  10 plot = Plot()
  11 ax = plot.gca(xscale='log', xlim=(10, 1500), xlabel='Frequency [Hz]',  yscale='log', ylim=(3e-24, 2e-20),  ylabel=r'Strain noise [1/$\sqrt{\mathrm{Hz}}$]')
  12 ax.plot_mmm(median, low, high, color='gwpy:ligo-hanford')
  13 ax.set_title('LIGO-Hanford strain noise variation around GW170817', fontsize=16)
  14 plot.show()

https://gwpy.github.io/docs/stable/_images/percentiles-4.png

パーセンタイルではなくmeanとσで表したい場合は、Spectrogramの2次元配列に対して時間軸(0軸)方向にmean, stdを取ればよい。

   1 mean  = FrequencySeries(data=sg.mean(0), frequencies=SG.frequencies)
   2 sigma = FrequencySeries(data=sg.std(0),  frequencies=SG.frequencies)

matplotlib色々

プロットの複雑なレイアウトはGridSpecが便利かも、という話

matplotlibで一定区間に背景色をつける方法

KAGRA/Subgroups/PEM/PythonMemoJP/plot (last edited 2021-08-11 15:28:39 by tatsuki.washimi)