0

My dataset consists of four variables (x, a, b, c), of which the latter three depend on the first. I want to show these in a single plot: three panels next to each other with plots of x against a, x against b, x against d, respectively. Now, since x is the same for every plot, I want the ticklabels to show up only on the leftmost panel, so I used ax.set_yticklabels([]) to remove them from the second and third panel. Here is the problem though: I want the three panels to have exactly the same width. However, the width of the plot is changed when adding or removing the tick labels. Also, I would like to the three panels to be exactly centred. So far I have been calculating the exact positions of each axes to satisfy this, but I don't know the width of the ticklabels + ylabel and I was wondering if there is a more elegant way of solving this issue.

Many thanks!

T

Example:

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_axes(0, 0, 0.3, 1])
ax1.plot(a, x)
ax1.xaxis.set_tick_params(which='major', size=5, width=1, direction='in', top='on')
ax1.yaxis.set_tick_params(which='major', size=5, width=1, direction='in', top='on')
ax1.invert_yaxis()
ax1.grid(ls=':')

ax2 = fig.add_axes([1/3+1/30, 0, 0.3, 1])
ax2.plot(b, x)
ax2.set_yticklabels([])
ax2.xaxis.set_tick_params(which='major', size=5, width=1, direction='in', top='on')
ax2.yaxis.set_tick_params(which='major', size=5, width=1, direction='in', top='on')
ax2.invert_yaxis()
ax2.grid(ls=':')

ax3 = fig.add_axes([2/3+1/30, 0, 0.3, 1])
ax3.plot(c, x)
ax3.set_yticklabels([])
ax3.xaxis.set_tick_params(which='major', size=5, width=1, direction='in', top='on')
ax3.yaxis.set_tick_params(which='major', size=5, width=1, direction='in', top='on')
ax3.invert_yaxis()
ax3.grid(ls=':')

plt.show()
Geo_toto
  • 23
  • 4
  • 1
    Welcome to Stack Overflow! Please take a moment to read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). You need to provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) that includes a toy dataset (refer to [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)). Please show what you've tried so far, why it is not satisfactory, and a clear description of what the desired output should be – Diziet Asahi Nov 02 '20 at 10:30
  • Have you considered making a 3x1 matplotlib subplot with shared x axis? Check out this [link](https://matplotlib.org/3.1.1/gallery/subplots_axes_and_figures/shared_axis_demo.html) – Jacob K Nov 02 '20 at 22:59

0 Answers0