matplotlib

Matplotlib中怎么设置图例的边框样式

小亿
104
2024-05-11 19:33:51
栏目: 编程语言

在Matplotlib中设置图例的边框样式可以通过调整legend对象的属性来实现。可以使用legend对象的set_frame_on方法来控制是否显示图例的边框,以及使用set_frame_linewidthset_frame_edgecolor等方法来设置边框的线宽和颜色。

以下是一个示例代码,演示如何设置图例的边框样式:

import matplotlib.pyplot as plt

# 创建一个简单的图例
plt.plot([1, 2, 3], label='Line 1')
plt.plot([3, 2, 1], label='Line 2')
plt.legend()

# 获取图例对象
legend = plt.gca().get_legend()

# 设置图例的边框样式
legend.set_frame_on(True)  # 显示图例边框
legend.get_frame().set_linewidth(1)  # 设置边框线宽
legend.get_frame().set_edgecolor('red')  # 设置边框颜色

plt.show()

在上面的示例代码中,set_frame_on(True)表示显示图例的边框,set_linewidth(1)设置边框的线宽为1,set_edgecolor('red')设置边框的颜色为红色。您可以根据需要调整这些参数来设置图例的边框样式。

0
看了该问题的人还看了