[英]Seaborn plots appending legends when plotting multiple plots in the same script
我是 Seaborn 的新手。 在同一个脚本中绘制多个图时,第一个图是正确的,但对于其余图,附加的图例会扭曲图。
我的代码
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')
我不确定我是否应该每次都使用sns.set()
。 我已经重命名了每个斧头,但问题仍然存在。
另外,也许你可以建议我如何改进我的情节。
谢谢你的建议。
我不确定这是否会解决您的问题。 但总的来说,每当在 matplotlib/seaborn 中创建多个绘图时,我都强烈倾向于使用显式面向对象的方法(matplotlib 是底层库,seaborn 只是包装它以使某些应用程序更快)。 这意味着摆脱ax.get_figure().savefig
部分。 我发现本教程在理解面向对象的 matplotlib 方法与隐式状态方法相比非常有用。
您在此方法中的代码如下所示:
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.