Home > Software engineering >  Matplotlib - stop/limit x axis to 100% (instead of bleeding over into 105) when using MultipleLocato
Matplotlib - stop/limit x axis to 100% (instead of bleeding over into 105) when using MultipleLocato

Time:08-02

The following code :

fig, ax = plt.subplots(figsize=(10, 5))
np.random.seed(1)
df = pd.DataFrame(
    {
        "x": np.random.randint(0, 4, size=(1000)),
        "y": np.random.randint(0, 4, size=1000),
    }
)
pd.crosstab(df["x"], df["y"], normalize="columns").mul(100).T.plot.barh(
    stacked=True, ax=ax
)
ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(base=5))

Will output:

enter image description here

If the line

ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(base=5))

Is removed, the limit is as expected (100) :

enter image description here

How can MultipleLocator be used in this case, without pushing the axis over the limit of 100 ?


CodePudding user response:

As r-beginners already pointed out in the comment, you can use enter image description here

  • Related