matplotlib サブプロットの行タイトル 質問する

matplotlib サブプロットの行タイトル 質問する

matplotlib では、図全体に設定されたタイトルと個々のプロットに設定されたタイトルに加えて、サブプロットの各行に個別のタイトルを設定することは可能ですか? これは、下の図のオレンジ色のテキストに対応します。ここに画像の説明を入力してください

そうでない場合、この問題をどのように回避しますか? 左側に空のサブプロットの別の列を作成し、オレンジ色のテキストで埋めますか?

text()またはを使用して各タイトルを手動で配置できることはわかっていますannotate()が、通常は多くの調整が必要であり、サブプロットも多数あります。よりスムーズな解決策はありますか?

ベストアンサー1

matplotlib 3.4.0 の新機能

行タイトルは次のように実装できるようになりました。サブ図の字幕:

新しいサブフィギュア機能により、ローカライズされたアーティスト(カラーバーや字幕) それ各サブ図にのみ関係する

見るサブ図をプロットする方法詳細についてはこちらをご覧ください。


OPの参考図を再現する方法:

  • どちらかFigure.subfigures(最もわかりやすい)

    fig.subfiguresそれぞれが独自subfigの 1x3 を持つ3x1 を作成しますsubfig.subplotssubfig.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.subplotsadd_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}')
    

どちらの例の出力も(スタイル設定後):

おすすめ記事