Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Subplot title does not obey padding #21394

Closed
cipriantrofin opened this issue Oct 20, 2021 · 2 comments · Fixed by #21398
Closed

[Bug]: Subplot title does not obey padding #21394

cipriantrofin opened this issue Oct 20, 2021 · 2 comments · Fixed by #21398

Comments

@cipriantrofin
Copy link

Bug summary

Last subplot title does not obey padding.

Code for reproduction

import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({ "Austria" : [1,2,3,4,5],
                    "Belgium" : [6,7,8,9,10],
                    "France" : [11,12,13,14,15] },
                   index = pd.to_datetime(['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04', '2000-01-05']))

countryList = df.columns

fig, axs = plt.subplots(len(countryList), 1, figsize = (15, len(countryList) * 5), sharex = True)

for i, c in enumerate(countryList):
    axs[i].bar(df.index, df[c])
    axs[i].set_title(c, pad=-14)

Actual outcome

subplots

Expected outcome

"France" subplot title should be aligned as the previous ones.

Operating system

Windows 10 x64

Matplotlib Version

3.4.3

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

Python 3.8.5

Jupyter version

jupyter notebook v6.4.5

Other libraries

No response

Installation

pip

Conda channel

No response

@jklymak
Copy link
Member

jklymak commented Oct 20, 2021

As a workaround you can do

      axs[i].set_title(c, pad=-14, y=1.001)

The problem here is that the automatic title placement is interfering. However, I admit I'm a little flummoxed why it is only a problem for the last title.

I think there is a reasonable argument that if the user specifies pad or y they mean for the title to be placed manually.

@jklymak
Copy link
Member

jklymak commented Oct 20, 2021

So this is a bit of a funny one. If you remove sharex=True, none of them are placed with the pad, which is expected behaviour, if a bit undesirable. Basically, the idea of the automatic placement of titles is that you don't want it to overlap axes. So it adjusts upwards, even if pad=-40 until it no longer overlaps.

However, if you set sharex=True the axes without xticks and xlabel have the axes position determined by ax.get_window_extent which at this point in the draw cycle is "None". That leads to the first two axes looking like they are obeying the pad.

@jklymak jklymak linked a pull request Oct 21, 2021 that will close this issue
7 tasks
@QuLogic QuLogic added this to the v3.6.0 milestone Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants