Tick ​​frequency when using boat box with marine / matplotlib

I draw with a marine box a series of boxes with

sns.boxplot(full_array)

      

where full_array

contains 200 arrays. So I have 200 boxes and ticks on the x-axis from 0 to 200.

xticks are too close to each other and I would like to show only a few of them, for example, the xtick tagged every 20 or so.

I tried several solutions mentioned here but they didn't work.

Every time I tried xticks I get wrong tick labels as they are numbered from 0 to N with a one-to-one spacing.

For example, with a line, ax.xaxis.set_major_locator(ticker.MultipleLocator(20))

I get a tagged xtick every 20, but the labels are 1, 2, 3, 4 instead of 20, 40, 60, 80 ...

Thanks to everyone who is so kind to help.

+3


source to share


1 answer


The marine box uses FixedLocator and FixedFormatter, i.e.

print ax.xaxis.get_major_locator()
print ax.xaxis.get_major_formatter()

      

prints

<matplotlib.ticker.FixedLocator object at 0x000000001FE0D668>
<matplotlib.ticker.FixedFormatter object at 0x000000001FD67B00>

      



Therefore, it is not enough to set the locator to MultipleLocator

, since the tick values ​​will still be set by fixed formatting.

Instead, you want to install ScalarFormatter

which sets the labels to match the numbers in their position.

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

ax = sns.boxplot(data = np.random.rand(20,30))

ax.xaxis.set_major_locator(ticker.MultipleLocator(5))
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())

plt.show()

      

enter image description here

+5


source







All Articles