Seaborn despine () returns ytick tags

Here is a snippet of code

tips = sns.load_dataset("tips") g = sns.FacetGrid(tips, col = 'time') g = g.map(plt.hist, "tip") 

with the next exit

enter image description here

I want to introduce a despine offset to these graphs while keeping the rest unchanged. So I inserted the despine function into the existing code:

 tips = sns.load_dataset("tips") g = sns.FacetGrid(tips, col = 'time') g.despine(offset=10) g = g.map(plt.hist, "tip") 

leading to the following graphs

enter image description here

As a result, the offset is applied to the axes. However, the shortcuts on the right chart are back, which I don’t want.

Can someone help me with this?

+5
source share
1 answer

To remove yaxis tag labels, you can use the following code:

The libs:

 import seaborn as sns sns.set_style('ticks') 

Adjusted Code:

 tips = sns.load_dataset("tips") g = sns.FacetGrid(tips, col = 'time') g.despine(offset=10) g = g.map(plt.hist, "tip") # IMPORTANT: I assume that you use colwrap=None in FacetGrid constructor # loop over the non-left axes: for ax in g.axes[:, 1:].flat: # get the yticklabels from the axis and set visibility to False for label in ax.get_yticklabels(): label.set_visible(False) ax.yaxis.offsetText.set_visible(False) 

enter image description here

A bit more general, the image now has a 2x2 FacetGrid, you want to interrupt with an offset, but x and yticklabels are returning:

enter image description here

Delete them all with this code:

 tips = sns.load_dataset("tips") g = sns.FacetGrid(tips, col = 'time', row='sex') g.despine(offset=10) g = g.map(plt.hist, "tip") # IMPORTANT: I assume that you use colwrap=None in FacetGrid constructor # loop over the non-left axes: for ax in g.axes[:, 1:].flat: # get the yticklabels from the axis and set visibility to False for label in ax.get_yticklabels(): label.set_visible(False) ax.yaxis.offsetText.set_visible(False) # loop over the top axes: for ax in g.axes[:-1, :].flat: # get the xticklabels from the axis and set visibility to False for label in ax.get_xticklabels(): label.set_visible(False) ax.xaxis.offsetText.set_visible(False) 

enter image description here

UPDATE:

for completeness, mwaskom ( ref to github issue ) gave an explanation of why this problem occurs:

So this is because matplotlib calls the .reset_ticks () axis inside, moving the spine. Otherwise, the spine moves, but the tics remain in one place. It is not configurable in matplotlib and even if it is, I don’t know if there is a public API for moving individual ticks. Unfortunately, I think that you will have to remove the tick marks yourself after correcting the spikes.

+1
source

All Articles