Search code examples
pythonpandasnumpymatplotlibbar-chart

matplotlib: break axis and scale unevenly


I have a bar-chart that needs to be broken along the x-axis, and after the break the scale of the x-axis should change.

The following code utilizes the brokenaxes package (https://github.com/bendichter/brokenaxes).

import sys
import matplotlib.pyplot as plt
from brokenaxes import brokenaxes
import pandas as pd
import numpy as np

df = pd.DataFrame()
df['A'] = pd.Series(np.random.randint(0,10,size=(5)))
df['B'] = pd.Series(np.random.randint(50,90,size=(5)))
df = df.sort_values('A', ascending=True)

bax = brokenaxes(xlims=((0, 11), (50, 90)))
bax.barh(np.arange(0, len(df)), df['A'], height=0.3, hatch='/')
bax.barh(np.arange(0.3, len(df) + 0.3), df['B'], height=0.3, hatch='\\')
bax.set_xlabel('value')
bax.set_ylabel('index')
plt.show()

The output is as below:

enter image description here

Currently the x-axis is scaled evenly. I would like to change the x-axis scale such that 0-10 should occupy more space (left half maybe?), and 50-90 should occupy less space (right half).


Solution

  • You could try

    brokenaxes(xlims=((0, 11), (50, 90)), width_ratios=[1,1])
    

    for equal distribution of the subplots.