简体   繁体   English

在同一脚本中绘制多个图时,Seaborn 会附加图例

[英]Seaborn plots appending legends when plotting multiple plots in the same script

I am new to Seaborn.我是 Seaborn 的新手。 When plotting multiple plots in the same script, the first plot is correct, but for the rest, the legends are appended which skew the plots.在同一个脚本中绘制多个图时,第一个图是正确的,但对于其余图,附加的图例会扭曲图。

My code我的代码

sns.set()
cmap = sns.cubehelix_palette(rot=-.2, as_cmap=True)
ax = sns.scatterplot(x="Clicks", y="Impressions",
                     hue="Language2", size="CTR",
                     palette=cmap, sizes=(10, 200),
                     data=df)
ax.get_figure().savefig('Test plot.png')

sns.set()
cmap = sns.cubehelix_palette(rot=-.2, as_cmap=True)
ax0 = sns.scatterplot(x="Impressions", y="Clicks",
                      hue="Word2", size="Transactions",
                      palette=cmap, sizes=(10, 200),
                      data=df)
ax0.get_figure().savefig('Test plot 2.png')

sns.set()
cmap = sns.cubehelix_palette(rot=-.2, as_cmap=True)
ax1 = sns.scatterplot(x="CTR", y="CostPerTransaction",
                      hue="Language2", size="Transactions",
                      palette=cmap, sizes=(10, 200),
                      data=df)
ax1.get_figure().savefig('Test plot 3.png')

I am not sure if I should use sns.set() each time.我不确定我是否应该每次都使用sns.set() I've renamed each ax but the issue persists.我已经重命名了每个斧头,但问题仍然存在。

情节一

情节二

Also, maybe you could suggest how I could improve my plots.另外,也许你可以建议我如何改进我的情节。

Thank you for your suggestions.谢谢你的建议。

I am not certain if this will fix your problem.我不确定这是否会解决您的问题。 But in general I have a strong preference for using the explicit object oriented approach whenever creating more than one plot in matplotlib/seaborn (matplotlib is the underlying library, seaborn is just wrapping it to make certain applications quicker).但总的来说,每当在 matplotlib/seaborn 中创建多个绘图时,我都强烈倾向于使用显式面向对象的方法(matplotlib 是底层库,seaborn 只是包装它以使某些应用程序更快)。 This means getting rid of the ax.get_figure().savefig parts.这意味着摆脱ax.get_figure().savefig部分。 I found this tutorial really useful in understanding the object oriented matplotlib approach compared to the implicit state approach.我发现本教程在理解面向对象的 matplotlib 方法与隐式状态方法相比非常有用。

Your code in this method would look like this:您在此方法中的代码如下所示:

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
cmap = sns.cubehelix_palette(rot=-.2, as_cmap=True)
fig1, ax1 = plt.subplots()
sns.scatterplot(x="Clicks", y="Impressions",
                     hue="Language2", size="CTR",
                     palette=cmap, sizes=(10, 200),
                     data=df,
                     ax=ax1)
# This may help with your axes labels spilling off the figure:
fig1.tight_layout()
fig1.savefig('Test plot.png')


#  the sns.set is not needed each time
fig2, ax2 = plt.subplots()
# cmap is the same, so we don't need to define that again
sns.scatterplot(x="Impressions", y="Clicks",
                      hue="Word2", size="Transactions",
                      palette=cmap, sizes=(10, 200),
                      data=df,
                      ax=ax2)
fig2.savefig('Test plot 2.png')

fig3, ax3 = plt.subplots()
sns.scatterplot(x="CTR", y="CostPerTransaction",
                      hue="Language2", size="Transactions",
                      palette=cmap, sizes=(10, 200),
                      data=df,
                      ax=ax3)
fig3.savefig('Test plot 3.png')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM