Matplotlib中怎么自定義圖例邊框

小億
107
2024-05-13 14:29:17

要自定義Matplotlib中圖例的邊框,可以使用Legend對(duì)象的屬性來(lái)設(shè)置。以下是一個(gè)簡(jiǎn)單的例子:

import matplotlib.pyplot as plt

# 創(chuàng)建圖例
plt.plot([1, 2, 3], label='Line 1')
plt.plot([3, 2, 1], label='Line 2')
legend = plt.legend()

# 設(shè)置圖例的邊框樣式
legend.get_frame().set_linewidth(2)  # 設(shè)置邊框?qū)挾?/span>
legend.get_frame().set_edgecolor('red')  # 設(shè)置邊框顏色

plt.show()

這段代碼會(huì)創(chuàng)建一個(gè)包含兩條線的圖例,并將其邊框的寬度設(shè)置為2,顏色設(shè)置為紅色。

除了設(shè)置邊框樣式外,還可以通過(guò)Legend對(duì)象的其他屬性來(lái)自定義圖例,如設(shè)置背景色、邊距等。更多詳細(xì)信息可以查看Matplotlib官方文檔。

0