Skip to content

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

Closed

Description

@cipriantrofin

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

Activity

  1. jklymak commented on Oct 20, 2021

    @jklymak
    Member

    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.

  2. jklymak commented on Oct 20, 2021

    @jklymak
    Member

    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.

  3. linked a pull request that will close this issueFIX: logic of title repositioning #21398on Oct 21, 2021
  4. added this to the v3.6.0 milestone on Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions