Skip to content

Instantly share code, notes, and snippets.

@spider-man-tm
Last active August 30, 2022 11:22
Show Gist options
  • Save spider-man-tm/011bf690404d83e20378010a002eb7f4 to your computer and use it in GitHub Desktop.
Save spider-man-tm/011bf690404d83e20378010a002eb7f4 to your computer and use it in GitHub Desktop.
凡例を枠外に出したプロットを保存する時に見切れない様にする方法
# matplotlibで画像として保存する
# 凡例をいい具合に枠外に持ってきた上で
# saveした時に見切れない様に調整
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4*math.pi, 100)
sin = np.sin(x)
cos = np.cos(x)
plt.plot(x, sin, label='sin curve', color='blue')
plt.plot(x, cos, label='cos curve', color='red')
# 凡例、右上枠外
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
# 見切れない様に
plt.savefig('figure1.png', bbox_inches='tight')
plt.close()
plt.plot(x, sin, label='sin curve', color='blue')
plt.plot(x, cos, label='cos curve', color='red')
# 凡例、左下枠外
plt.legend(bbox_to_anchor=(0, -0.1), loc='upper left', borderaxespad=0)
# 見切れない様に
plt.savefig('figure2.png', bbox_inches='tight')
plt.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment