本文介绍了删除此Seaborn人物中产生的两个传说之一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我刚刚开始使用seaborn来制作我的人物.但是,我似乎无法删除此处产生的传说之一.

I have just started using seaborn to produce my figures. However I can't seem to remove one of the legends produced here.

我正在尝试相互绘制两个精度并沿对角线绘制一条线,以使其更容易看出效果更好(如果有人能以更好的方式在seaborn中绘制此数据,请告诉我!).我想保留的图例是左侧的图例,它为"N_bands"显示不同的颜色,为主题编号"显示不同的形状

I am trying to plot two accuracies against each other and draw a line along the diagonal to make it easier to see which has performed better (if anyone has a better way of plotting this data in seaborn - let me know!). The legend I'd like to keep is the one on the left, that shows the different colours for 'N_bands' and different shapes for 'Subject No'

ax1 = sns.relplot(y='y',x='x',data=df,hue='N bands',legend='full',style='Subject No.',markers=['.','^','<','>','8','s','p','*','P','X','D','H','d']).set(ylim=(80,100),xlim=(80,100))
ax2 = sns.lineplot(x=range(80,110),y=range(80,110),legend='full')

我尝试将ax1和ax2的kwarg图例设置为"full","brief"和False(一起和分别),并且似乎只删除了左边的一个或两个.

I have tried setting the kwarg legend to 'full','brief' and False for both ax1 and ax2 (together and separately) and it only seems to remove the one on the left, or both.

我也尝试过使用matplotlib删除轴

I have also tried to remove the axes using matplotlib

ax1.ax.legend_.remove()
ax2.legend_.remove()

但这会导致相同的行为(左图例消失).

But this results in the same behaviour (left legend dissapearing).

更新:这是您可以自己运行的一个最小示例:

UPDATE: Here is a minimal example you can run yourself:

test_data = np.array([[1.,2.,100.,9.],[2.,1.,100.,8.],[3.,4.,200.,7.]])
test_df = pd.DataFrame(columns=['x','y','p','q'], data=test_data)

sns.set_context("paper")
ax1=sns.relplot(y='y',x='x',data=test_df,hue='p',style='q',markers=['.','^','<','>','8'],legend='full').set(ylim=(0,4),xlim=(0,4))
ax2=sns.lineplot(x=range(0,5),y=range(0,5),legend='full')

尽管由于正确的图例被涂上颜色,这不能完全重现错误(我不知道该如何重现此错误-创建我的数据框的方式会有所作为吗?).但是问题的实质仍然是-如何删除右侧的图例而保留左侧的图例?

Although this doesn't reproduce the error perfectly as the right legend is coloured (I have no idea how to reproduce this error then - does the way my dataframe was created make a difference?). But the essence of the problem remains - how do I remove the legend on the right but keep the one on the left?

推荐答案

您正在通过relplot生成的FacetGrid的(仅)轴上绘制线图.这是非常不常规的,因此可能会发生奇怪的事情.

You're plotting a lineplot in the (only) axes of a FacetGrid produced via relplot. That's quite unconventional, so strange things might happen.

一个删除FacetGrid的图例但将其保留在线图中的选项为

One option to remove the legend of the FacetGrid but keeping the one from the lineplot would be

g._legend.remove()

完整代码(我还在其中纠正了网格和轴的混乱命名)

Full code (where I also corrected for the confusing naming if grids and axes)

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

test_data = np.array([[1.,2.,100.,9.],[2.,1.,100.,8.],[3.,4.,200.,7.]])
test_df = pd.DataFrame(columns=['x','y','p','q'], data=test_data)

sns.set_context("paper")
g=sns.relplot(y='y',x='x',data=test_df,hue='p',style='q',markers=['.','^','<','>','8'], legend='full')

sns.lineplot(x=range(0,5),y=range(0,5),legend='full', ax=g.axes[0,0])

g._legend.remove()

plt.show()

请注意,这是一种破解,可能会在以后的Seaborn版本中破坏.

Note that this is kind of a hack, and it might break in future seaborn versions.

另一种选择是不在此处使用FacetGrid,而仅在一个轴上绘制散点图和折线图,

The other option is to not use a FacetGrid here, but just plot a scatter and a line plot in one axes,

ax1 = sns.scatterplot(y='y',x='x',data=test_df,hue='p',style='q',
                      markers=['.','^','<','>','8'], legend='full')

sns.lineplot(x=range(0,5),y=range(0,5), legend='full', ax=ax1)

plt.show()

这篇关于删除此Seaborn人物中产生的两个传说之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 10:57