更改标签后标记未显示在图例中(seaborn 和 matplotlib)
当我更改传奇标题时,我的两个标记中只有第一个是重新绘制的,我做错了什么,还是有解决方法?
我在下面运行了代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#create dataframe
np.random.seed(seed=10)
x = np.arange(0,20)
y1 = np.arange(0,20) + 10*np.random.rand(20)
y2 = np.arange(20,0,-1) + 10*np.random.rand(20)
mask = np.random.randint(low=0, high=2, size=20, dtype=bool)
df = pd.DataFrame()
df['x'] = x
df['y1'] = y1
df['y2'] = y2
df['mask'] = mask
#plot results
fig, axs = plt.subplots(2)
f1 = sns.scatterplot(data=df, y='y1', x='x', ax=axs[0], hue="mask")
f2 = sns.scatterplot(data=df, y='y2', x='x', ax=axs[1], hue="mask")
new_title = ""
new_labels = ['Include', 'Exclude']
for ax in axs:
ax.legend(title=new_title, labels=new_labels)
fig.savefig(r"C:\\Temp\error.png", bbox="tight")
结果在下面。如您所见,图例中的“ Include”标签具有标记,但“排除”标签不会
When I change the legend titles, only the first of my two markers is redrawn, am I doing something wrong, or is there a workaround?
I ran the code below:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#create dataframe
np.random.seed(seed=10)
x = np.arange(0,20)
y1 = np.arange(0,20) + 10*np.random.rand(20)
y2 = np.arange(20,0,-1) + 10*np.random.rand(20)
mask = np.random.randint(low=0, high=2, size=20, dtype=bool)
df = pd.DataFrame()
df['x'] = x
df['y1'] = y1
df['y2'] = y2
df['mask'] = mask
#plot results
fig, axs = plt.subplots(2)
f1 = sns.scatterplot(data=df, y='y1', x='x', ax=axs[0], hue="mask")
f2 = sns.scatterplot(data=df, y='y2', x='x', ax=axs[1], hue="mask")
new_title = ""
new_labels = ['Include', 'Exclude']
for ax in axs:
ax.legend(title=new_title, labels=new_labels)
fig.savefig(r"C:\\Temp\error.png", bbox="tight")
The result is below. As you can see the "Include" label in the legend has a marker, but the "Exclude" label does not
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论