📜  删除 Matplotlib 中的图例边框

📅  最后修改于: 2022-05-13 01:54:37.587000             🧑  作者: Mango

删除 Matplotlib 中的图例边框

在本文中,我们将学习如何在 Matplotlib 中移除图例边框。让我们讨论一些概念:

  • 图例是描述图形元素的区域。在 matplotlib 库中,有一个名为 legend() 的函数,用于在轴上放置图例。

方法:

  1. 导入库 (Matplotlib)
  2. 导入/创建数据。
  3. 绘制图表。
  4. 添加图例。
  5. 删除图例边框。

示例:这是一个带有图例的示例。

Python3
# importing packages
import numpy as np
import matplotlib.pyplot as plt
 
# create data
x = np.linspace(1, 10, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
 
# plot graph
plt.plot(x, y1)
plt.plot(x, y2)
 
# add legend
plt.legend(['Sine wave', 'Cos wave'])
plt.show()


Python3
# importing packages
import numpy as np
import matplotlib.pyplot as plt
 
# create data
x = np.linspace(1, 10, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
 
# plot graph
plt.plot(x, y1)
plt.plot(x, y2)
 
# add legend and remove frame
plt.legend(['Sine wave', 'Cos wave'], frameon=False)
plt.show()


Python3
# importing packages
import numpy as np
import matplotlib.pyplot as plt
 
# create data
x = np.linspace(1, 10, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
 
# plot graph
plt.plot(x, y1)
plt.plot(x, y2)
 
# add legend
leg = plt.legend(['Sine wave', 'Cos wave'])
 
# set opacity equal to zero i.e; transparent
leg.get_frame().set_alpha(0)
plt.show()


输出:

方法一:(使用frameon = False)

蟒蛇3

# importing packages
import numpy as np
import matplotlib.pyplot as plt
 
# create data
x = np.linspace(1, 10, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
 
# plot graph
plt.plot(x, y1)
plt.plot(x, y2)
 
# add legend and remove frame
plt.legend(['Sine wave', 'Cos wave'], frameon=False)
plt.show()

输出 :

方法二:(使用legend.get_frame().set_alpha(0))

蟒蛇3

# importing packages
import numpy as np
import matplotlib.pyplot as plt
 
# create data
x = np.linspace(1, 10, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
 
# plot graph
plt.plot(x, y1)
plt.plot(x, y2)
 
# add legend
leg = plt.legend(['Sine wave', 'Cos wave'])
 
# set opacity equal to zero i.e; transparent
leg.get_frame().set_alpha(0)
plt.show()

输出 :