matplotlib では、図全体に設定されたタイトルと個々のプロットに設定されたタイトルに加えて、サブプロットの各行に個別のタイトルを設定することは可能ですか? これは、下の図のオレンジ色のテキストに対応します。
そうでない場合、この問題をどのように回避しますか? 左側に空のサブプロットの別の列を作成し、オレンジ色のテキストで埋めますか?
text()
またはを使用して各タイトルを手動で配置できることはわかっていますannotate()
が、通常は多くの調整が必要であり、サブプロットも多数あります。よりスムーズな解決策はありますか?
ベストアンサー1
matplotlib 3.4.0 の新機能
行タイトルは次のように実装できるようになりました。サブ図の字幕:
新しいサブフィギュア機能により、ローカライズされたアーティスト(カラーバーや字幕) それ各サブ図にのみ関係する。
見るサブ図をプロットする方法詳細についてはこちらをご覧ください。
OPの参考図を再現する方法:
どちらか
Figure.subfigures
(最もわかりやすい)fig.subfigures
それぞれが独自subfig
の 1x3 を持つ3x1 を作成しますsubfig.subplots
。subfig.suptitle
fig = plt.figure(constrained_layout=True) fig.suptitle('Figure title') # create 3x1 subfigs subfigs = fig.subfigures(nrows=3, ncols=1) for row, subfig in enumerate(subfigs): subfig.suptitle(f'Subfigure title {row}') # create 1x3 subplots per subfig axs = subfig.subplots(nrows=1, ncols=3) for col, ax in enumerate(axs): ax.plot() ax.set_title(f'Plot title {col}')
または
Figure.add_subfigure
(既存のものへsubplots
)すでに 3x1 がある場合は
plt.subplots
、add_subfigure
基になる に入りますgridspec
。ここでも、それぞれがsubfig
独自の 1x3subfig.subplots
と を取得しますsubfig.suptitle
。# create 3x1 subplots fig, axs = plt.subplots(nrows=3, ncols=1, constrained_layout=True) fig.suptitle('Figure title') # clear subplots for ax in axs: ax.remove() # add subfigure per subplot gridspec = axs[0].get_subplotspec().get_gridspec() subfigs = [fig.add_subfigure(gs) for gs in gridspec] for row, subfig in enumerate(subfigs): subfig.suptitle(f'Subfigure title {row}') # create 1x3 subplots per subfig axs = subfig.subplots(nrows=1, ncols=3) for col, ax in enumerate(axs): ax.plot() ax.set_title(f'Plot title {col}')
どちらの例の出力も(スタイル設定後):