gpt4 book ai didi

python - Seaborn 不使用 %matplotlib 笔记本显示图例

转载 作者:行者123 更新时间:2023-12-01 02:27:03 28 4
gpt4 key购买 nike

当我使用%matplotlib笔记本时,图例不显示。

%matplotlib notebook
...
fg = seaborn.FacetGrid(data=df, hue='Scenario', hue_order=df.Scenario.unique().tolist(), aspect=1.61)
fg.map(plt.scatter, 'Yaw', 'posNoseEye').add_legend()

enter image description here

当我使用 %matplotlib inline 时,图例会正确显示。

%matplotlib inline
...
fg = seaborn.FacetGrid(data=df, hue='Scenario', hue_order=df.Scenario.unique().tolist(), aspect=1.61)
fg.map(plt.scatter, 'Yaw', 'posNoseEye').add_legend()

enter image description here

如何在 %matplotlib 笔记本中管理它?谢谢

由于我无法在评论中添加图片,所以我尝试了下面的答案,但仍然不起作用。我有最新版本的 matplotlib 和 seaborn。 enter image description here

最佳答案

该图没有任何用于图例的空间。有多种方法可以克服这个问题,如 this answer 所示。 。

一个选项是手动为图例创建一些空间,例如

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

df = pd.DataFrame(np.random.rand(20,2), columns=['Yaw', 'posNoseEye'])
df["Scenario"] = np.random.choice(["Scenario A","Scenario B"], size=20)

fg = sns.FacetGrid(data=df, hue='Scenario', hue_order=df.Scenario.unique().tolist(), aspect=1.61)
fg.map(plt.scatter, 'Yaw', 'posNoseEye')

fg.fig.legend()

fg.fig.subplots_adjust(right=0.7)
plt.show()

enter image description here

关于python - Seaborn 不使用 %matplotlib 笔记本显示图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47290299/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com