
前提・実現したいこと
matplotlibで下のように,グラフとその外側に凡例が表示されているようなグラフ画像を作成したいです.
(画像の縦横比を手動で加工して作成した画像です.)
凡例をグラフの外側に作成し,凡例が見切れないようにグラフの描画範囲を右側に拡大したところ,想定外の描画されたグラフが発生しました.
発生している問題・エラーメッセージ
該当のソースコード
def plot_loss_acc(loss_t, loss_v, acc_t, acc_v): plt.rc('font', family='serif') fig = plt.figure() ax1 = fig.add_subplot(1, 1, 1) ax2 = ax1.twinx() ax1.grid(True) ax2.grid(False) ax1.plot(loss_t, label='Training Loss', color='orange') ax1.plot(loss_v, label='Validation Loss', color='red') ax2.plot(acc_t, label='Training Acc', color='blue') ax2.plot(acc_v, label='Validation Acc', color='green') ax1.set_xlabel('Epoch') ax1.set_ylabel('Loss') ax2.set_ylabel('Accuracy') handler1, label1 = ax1.get_legend_handles_labels() handler2, label2 = ax2.get_legend_handles_labels() ax1.legend(handler1 + handler2, label1 + label2, loc='upper left', bbox_to_anchor=(1.1, 1)) plt.subplots_adjust(right=0.6) graph_fnameH = "hogehoge" plt.savefig(graph_fnameH + '.png', dpi=300) plt.show()
試したこと
plt.subplots_adjust(right=0.6)
を取り除いた場合,以下のように表示されます.右側に凡例が見切れていますが,このグラフの縦横比を維持しつつ右方向に描画範囲を拡大したいと考えています.
よろしくお願いいたします.

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/08/27 23:35