要将图例添加到matplotlib图例中,只需运行legend()。
如何从一个情节中移除一个传说?
(我最接近这一点的是运行legend([]),以便从数据中清空传说。但这在右上角留下了一个丑陋的白色矩形。)
要将图例添加到matplotlib图例中,只需运行legend()。
如何从一个情节中移除一个传说?
(我最接近这一点的是运行legend([]),以便从数据中清空传说。但这在右上角留下了一个丑陋的白色矩形。)
当前回答
下面是一个使用matplotlib和seaborn处理子图的图例删除和操作的更复杂的例子:
从seaborn获取由sns.<some_plot>()创建的Axes对象,并执行@naitsirhc指示的ax.get_legend().remove()。下面的例子还展示了如何将图例放在一边,以及如何在子情节的上下文中处理。
# imports
import seaborn as sns
import matplotlib.pyplot as plt
# get data
sns.set()
sns.set_theme(style="darkgrid")
tips = sns.load_dataset("tips")
# subplots
fig, axes = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(12,6))
fig.suptitle('Example of legend manipulations on subplots with seaborn')
g0 = sns.pointplot(ax=axes[0], data=tips, x="day", y="total_bill", hue="size")
g0.set(title="Pointplot with no legend")
g0.get_legend().remove() # <<< REMOVE LEGEND HERE
g1 = sns.swarmplot(ax=axes[1], data=tips, x="day", y="total_bill", hue="size")
g1.set(title="Swarmplot with legend aside")
# change legend position: https://www.statology.org/seaborn-legend-position/
g1.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)
其他回答
下面是一个使用matplotlib和seaborn处理子图的图例删除和操作的更复杂的例子:
从seaborn获取由sns.<some_plot>()创建的Axes对象,并执行@naitsirhc指示的ax.get_legend().remove()。下面的例子还展示了如何将图例放在一边,以及如何在子情节的上下文中处理。
# imports
import seaborn as sns
import matplotlib.pyplot as plt
# get data
sns.set()
sns.set_theme(style="darkgrid")
tips = sns.load_dataset("tips")
# subplots
fig, axes = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(12,6))
fig.suptitle('Example of legend manipulations on subplots with seaborn')
g0 = sns.pointplot(ax=axes[0], data=tips, x="day", y="total_bill", hue="size")
g0.set(title="Pointplot with no legend")
g0.get_legend().remove() # <<< REMOVE LEGEND HERE
g1 = sns.swarmplot(ax=axes[1], data=tips, x="day", y="total_bill", hue="size")
g1.set(title="Swarmplot with legend aside")
# change legend position: https://www.statology.org/seaborn-legend-position/
g1.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)
如果你把pyplot称作PLT
frameon=False是删除图例周围的边框
“传递的信息是,图例中不应该有变量
import matplotlib.pyplot as plt
plt.legend('',frameon=False)
你可以简单地这样做:
中心——axs [n]。传奇(loc =“左上”,ncol = 2, labelspacing = 0.01)
(我的4、5、6,7,8,9,10,11,12日,13日,14日,15日,16日,17日,18日19): 中心——axs[我].legend ([])
如果你不使用fig和ax plot对象,你可以这样做:
import matplotlib.pyplot as plt
# do plot specifics
plt.legend('')
plt.show()
从matplotlib v1.4.0rc4开始,删除方法已经添加到图例对象中。
用法:
ax.get_legend().remove()
or
legend = ax.legend(...)
...
legend.remove()
请参见这里介绍的提交。