可以通过设置legend的边框颜色为'none'或者alpha值为0来去除边框,示例代码如下:

import matplotlib.pyplot as plt

# 生成数据
x = [1, 2, 3, 4, 5]
y1 = [1, 2, 3, 4, 5]
y2 = [1, 4, 9, 16, 25]

# 绘制图像
plt.plot(x, y1, label='y=x')
plt.plot(x, y2, label='y=x^2')

# 设置legend
plt.legend(frameon=False)  # 去除边框

# 显示图像
plt.show()

另外,如果想要去除整个图像的边框,可以使用ax.spines将图像边框设置为'none',示例代码如下:

import matplotlib.pyplot as plt

# 生成数据
x = [1, 2, 3, 4, 5]
y1 = [1, 2, 3, 4, 5]
y2 = [1, 4, 9, 16, 25]

# 绘制图像
fig, ax = plt.subplots()
ax.plot(x, y1, label='y=x')
ax.plot(x, y2, label='y=x^2')

# 设置legend
ax.legend(frameon=False)  # 去除边框

# 去除图像边框
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

# 显示图像
plt.show()
pltlegend去除边框

原文地址: https://www.cveoy.top/t/topic/bxAo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录