0

when using boxplot with matplotlib , I wouldlike to have the x ticks to adapt to the figure size, so that the x-axis is not completely unreadable

Example

import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform(size = (20, 50))
fig, ax = plt.subplots()
ax.boxplot(x, positions=np.arange(1, x.shape[1]+1))

which gives me

enter image description here

I expected the tick labels to adapt as for a regular plot, but apparently not. Any workarounds?

2
  • 2
    Seaborn's boxplot with native_scale=True would give you a numeric x-axis. Commented Sep 6, 2024 at 15:39
  • E.g. sns.boxplot(x, native_scale=True) example plot. Or sns.boxplot(pd.DataFrame(x, columns=np.arange(1, x.shape[1]+1)), native_scale=True) to start the numbering at 1. Commented Sep 6, 2024 at 16:21

1 Answer 1

1

Boxplot uses a FixedLocator to determine the tick locations, change it to AutoLocator:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoLocator, ScalarFormatter

x = np.random.uniform(size = (20, 50))
fig, ax = plt.subplots()
ax.boxplot(x, positions=np.arange(1, x.shape[1]+1))
ax.xaxis.set_major_locator(AutoLocator())
ax.xaxis.set_major_formatter(ScalarFormatter())

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.