如何停止Seapine Despine移除第二轴上的y刻度

时间:2020-05-27 12:12:28

标签: python matplotlib seaborn

在使用seaborn的despine()时遇到问题。我试图使用两个y轴,并且只删除每个轴的必要棘刺,但是despine()也会删除右侧垂直轴的刻度线。最小的工作示例如下:

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

seaborn.set()
seaborn.set_style("ticks")

fig, ax1 = plt.subplots(figsize=(6,2))
ax2 = ax1.twinx()

ax1.plot(np.array([0,1,3]))
ax2.plot(np.array([2,2,1]))

seaborn.despine(ax=ax2, left=True, right=False, trim="True")
seaborn.despine(ax=ax1, bottom=True, left=False, right=True, trim="true")

plt.show()

如下图所示,这也删除了右侧垂直轴上的刻度线(我不想要)。

enter image description here

任何对我如何解决此问题的见解都将受到高度赞赏!

1 个答案:

答案 0 :(得分:3)

实施twinx的方式中,第二个y轴上的刻度实际上属于“左”脊椎,因此您需要

seaborn.despine(ax=ax2, left=False, right=False, trim="True")