lmiguelvargasf lmiguelvargasf - 1 month ago 8
Python Question

Matplotlib: Sharing axes when having 3 graphs 2 at the left and 1 at the right

I have following graph:
enter image description here

However, I want that graphs 221 and 223 share the same x axis. I have the following code:

self.fig_part_1 = plt.figure()
self.plots_part_1 = [
plt.subplot(221),
plt.subplot(223),
plt.subplot(122),
]


How can I achieve that? In the end I do not want the numbers of axis x in plot 221 to be shown.

Answer

Just use plt.subplots (different from plt.subplot) to define all your axes, with the option sharex=True:

f, axes = plt.subplots(2,2, sharex=True)
plt.subplot(122)
plt.show()

Note that the second call with larger subplot array overlay the preceding one.

Example (could not display image due to reputation...)