1

Matplotlib's broken axis example has a break on the left and the right side of the frame and is placed exactly in the middle of the y-axis.

enter image description here

  1. How can I create an unbroken line on the right border and a broken axis on the left border?
  2. Is there a keyword argument to manually adjust the horizontal whitespace in the broken axis?
  3. Is it possible to decide where on the y-axis the break will be placed? For example, at 80% from the top border, or 20% from the top border?

Any help is greatly appreciated.

  • The structure of the interrupted axis is created by deleting the top and bottom edges of the two graphs, so I have never seen a graph with only one side. The location of the interruption can be adjusted by the ratio of the height of the graphs. – r-beginners Jun 25 '21 at 12:31
  • This [answer](https://stackoverflow.com/questions/5656798/python-matplotlib-is-there-a-way-to-make-a-discontinuous-axis) is an example where the right axis is deleted and only the left one is used. – r-beginners Jun 25 '21 at 12:43
  • I think I can do it by installing [brokenaxes](https://github.com/bendichter/brokenaxes) library. – r-beginners Jun 25 '21 at 12:50
  • IIUC, the answer you link to doesn't exactly solve my issue. It breaks the x-axis instead of the y-axis, but it does that on the top and bottom line of the border/frame. In this case, I am looking for a way that it breaks only the top frame or the bottom frame. – madman_with_a_box Jun 25 '21 at 13:33

1 Answers1

1
  1. Not breaking the right axis Since the way to break the axis is to make two sets of axes, the only way I can think of the do this is to draw a line going from the top right corner of the lower axis to the bottom right corner of the upper axis. This can be done using transforms to get the relevant corners of the two axes in the axes coordinates of one of the axes
# Get the points from the lower-right corner of ax1 
# to the top-right corner of ax2 in the axes coordinates
# of ax1
low = (1, 0)
high = ax1.transAxes.inverted().transform(
    ax2.transAxes.transform((1, 1))
)

and then plotting them with transform=ax1.transAxes

ax1.plot(
    *list(zip(low, high)), "-k", 
    transform=ax1.transAxes, clip_on=False,   
    lw=ax1.spines.right.get_linewidth(),
)

2 and 3. Adjust the whitespace and position of the break This can be achieved using any way of creating different axes in matplotlib, such as passing gridspec_kw to plt.subplots e.g.

gridspec_kw = dict(height_ratios=(1, 4), hspace=0.30)
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, gridspec_kw=gridspec_kw)

As the name suggest height_ratios defines the ratios of the heights of the axes. hspace sets the vertical space between the two sets of axes (if you want to break the x-axis instead you can use wspace)

Applying these changes to the example you link, and removing the diagonal lines on the right hand side by changing

ax1.plot([0, 1], [0, 0], transform=ax1.transAxes, **kwargs)
ax2.plot([0, 1], [1, 1], transform=ax2.transAxes, **kwargs)

to

ax1.plot(0, 0, transform=ax1.transAxes, **kwargs)
ax2.plot(0, 1, transform=ax2.transAxes, **kwargs)

you get the following plot enter image description here

tomjn
  • 5,100
  • 1
  • 9
  • 24
  • I tested your suggestions. 1. How did you arrive at the coordinates `(1,1)` and `(1,1.2)`? 2. `ax2.plot(...` does indeed fill the gap between the two subplots but does not remove the diagonal lines. – madman_with_a_box Jun 25 '21 at 14:21
  • Ah sorry you also need to remove the diagonal lines. I've edited answer to include the lines you need to change to remove those. The 1.2 means 20% higher than the height of the lower y-axis. I just chose this by eye. I'm sure the exact values could be found using transforms if they are needed. – tomjn Jun 25 '21 at 14:39
  • Thanks very much! I'm accepting your answer. Also, can I please trouble you for how to get the exact values from transforms? Or if you could point to me the relevant docs. I don't really understand. – madman_with_a_box Jun 25 '21 at 14:45
  • I was interested to check myself so I changed the first part to use the transforms (and linked the tutorial) – tomjn Jun 25 '21 at 14:51
  • I am unable to reproduce the line using transforms (after setting `transform=None`). It should work because its providing coordinates to `ax2.plot` and I checked that the color of the line wasn't accidentally set to white or that linewidth isn't 0. – madman_with_a_box Jun 25 '21 at 15:07
  • I tested it and it worked fine for me. I also tried changing the line to red so I could see it and it looked correct. – tomjn Jun 25 '21 at 15:12