Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn despine with two y-scales (twinx)

How can I keep seaborn.despine from putting both of my y-scales onto the left side of my plot? The best I've come up with so far is:

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)

But any other combination will either not despine the y-axes or put the right axis onto the left.

Output of the above: (desired output has no spines, just numbers on left and right)

enter image description here

like image 527
Nils Gudat Avatar asked Mar 12 '23 04:03

Nils Gudat


1 Answers

I guess that's what you want then.

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

like image 187
giosans Avatar answered Mar 15 '23 06:03

giosans