import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x), label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)")
plt.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')
# plt.tight_layout()
plt.show()

plt.tight_layout()
沒加這一行
Spyder沒出現裁切圖例的狀況
但是 plt.savefig(“bbox_to_anchor.png”)
儲存的檔案出現被裁切的狀況:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x), label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)")
lg = plt.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')
plt.savefig('example.png',
dpi=300,
format='png',
bbox_extra_artists=(lg,),
bbox_inches='tight')

bbox_extra_artists 指定 Artist 的列表,該列表在計算緊湊 bbox 時會考慮在內。
如果將 bbox_inches 設定為 tight,它將計算出圖中的緊湊型 bbox。
推薦hahow線上學習python: https://igrape.net/30afN



![Python TQC考題604 眾數, cnt[L.index(n)]+=1, L[cnt.index(max(cnt))], if L.count(n)>maxcnt: Python TQC考題604 眾數, cnt[L.index(n)]+=1, L[cnt.index(max(cnt))], if L.count(n)>maxcnt:](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2022/04/20220430181911_73.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python TQC考題404 數字反轉判斷,n_rev=n[::-1], list1.reverse() Python TQC考題404 數字反轉判斷,n_rev=n[::-1], list1.reverse()](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2022/04/20220825152414_97.png?quality=90&zoom=2&ssl=1&resize=350%2C233)




近期留言