Skip to content
Advertisement

Line up plots between two separate axis with matplotlib

I have the following plot

JavaScript

Which looks as :

enter image description here

I would like to arrange the patches on the right plot (ax_dict['b']) so that they’re horizontally aligned with the bars from ax_dict['a'].

Currently they’re roughly inline – but the bars are higher / lower on the right than the bars on the left (red circles indicate the “gaps”, which wouldn’t be present if they were lined up exactly):

enter image description here

My question is – how can i create this plot so that the bars line up exactly ?

Advertisement

Answer

This issue can easily be fixed by giving both axes the same y limits, in your example this might be done simply by adding:

JavaScript

for each subplot, i.e. once before and once after the line ax = ax_dict["b"].

Sidenote: you should consider storing each unique axis within a unique variable to avoid confusion, e.g. naming them ax_a and ax_b.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement