Seaborn去除双y轴(twinx)的边框

6

如何防止seaborn.despine将我的两个y轴放在绘图的左侧? 到目前为止,我想到的最好方法是:

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

sns.set_style("white")

fig, ax = plt.subplots()
ax.plot(np.random.rand(10))
ax2 =ax.twinx()
ax2.plot(100*np.random.rand(10))
sns.despine(ax=ax, right=True, left=True)
sns.despine(ax=ax2, left=True, right=False)

但是任何其他组合要么不会去掉y轴的脊柱,要么会将右轴放到左侧。

以上输出结果应该是没有脊柱的,左右两边只有数字。

enter image description here


1
我不确定我是否理解正确,因为如果我运行你的代码片段,我会在一侧得到一个y轴刻度和在另一侧得到另一个y轴刻度。这不是你想要的吗? - giosans
也许你可以添加一个图形。 - giosans
说得好 - 我在问题中没有设置 seaborn 样式,这使得去除轴边框是不必要的! - Nils Gudat
1个回答

9
我想那就是你想要的了。
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

sns.set_style("white")

fig, ax = plt.subplots()
ax.plot(np.random.rand(10))
ax2 =ax.twinx()
ax2.plot(100*np.random.rand(10))
sns.despine(ax=ax, right=True, left=True)
sns.despine(ax=ax2, left=True, right=False)
ax2.spines['right'].set_color('white')

no spines, just numbers on left and right


2
如果您想在两侧显示轴线(这正是我想要的),则可以按如下方式操作:sns.despine(ax=ax, right=True, left=False) sns.despine(ax=ax2, left=True, right=False) - Dänu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接