Last active
August 30, 2022 11:22
-
-
Save spider-man-tm/011bf690404d83e20378010a002eb7f4 to your computer and use it in GitHub Desktop.
凡例を枠外に出したプロットを保存する時に見切れない様にする方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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