Skip to content

tight_layout is incorrect for certain title sizes #8201

Closed

Description

@retsyo

I am using Python 3.4.4 |Anaconda 2.3.0 (64-bit) on win64 bits, with latest matplotlib and pyqt updated by conda install xx
for the following code, if I use unicode title in the for-loop, the images are too small. However, if I use English title, the images' size are expected

This can be observed for both TkAgg and Qt5Agg

#coding=utf-8
import matplotlib
matplotlib.rcParams['backend'] = 'TkAgg'
#~ matplotlib.rcParams['backend'] = 'Qt5Agg'

from pylab import *
import PIL.Image as Image

im=Image.open('Mario cosplay.jpg').convert('L')

orgsize=im.size
newsize=[int(i/4) for i in orgsize]

subplot(2,3,1)
imshow(im, cmap='gray', vmin=0, vmax=255)
title('original pic', fontsize=20);

filters=[Image.NEAREST, Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]
filtersName=['NEAREST', 'BILINEAR', 'BICUBIC', 'ANTIALIAS']
for k in range(len(filters)):
    subplot(2,3,k+2)
    im1=im.resize(newsize, filters[k])
    imshow(im1, cmap='gray', vmin=0, vmax=255)
    #~ title('原图的1/%i,插值法:%s' %  (4, filtersName[k]), fontsize=20)       # bug here
    title('size: 1/%i,interpolation: %s' %  (4, filtersName[k]), fontsize=20)    # no bug

gcf().tight_layout()

show()

unicode_title
english_title

Activity

  1. changed the title [-]unicode title does not go well with gcf().tight_layout(), which yields bad image size[/-] [+]tight_layout is incorrect for certain title sizes[/+] on Apr 22, 2020
  2. anntzer commented on Apr 22, 2020

    @anntzer
    Contributor

    This actually does have anything to do with unicode. It's just that for certain title lengths, tight_layout shrinks the axes bbox in a vain attempt of decreasing the title size as well, but of course that doesn't work; for even longer titles it just gives up and doesn't shrink the axes.

    Minimalish repro:

    from pylab import *
    
    fig, axs = subplots(2, 3)
    for ax in axs.flat:
        ax.imshow([[0]])
        ax.set_title("so long tight_layout() just gives up", fontsize=20)
    fig.tight_layout()
    
    fig, axs = subplots(2, 3)
    for ax in axs.flat:
        ax.imshow([[0]])
        ax.set_title("just long enough to fail", fontsize=20)
    fig.tight_layout()
    
    show()

    1
    2

  3. jklymak commented on Apr 22, 2020

    @jklymak
    Member

    So, I guess its a valid question if tight_layout and constrained_layout should even care about the title or xlabel for the x-direction calculation, or the ylabel for y-direction calculation. Shrinking the axes doesn't make more room. That makes the code more complicated, but maybe not terribly so.

  4. anntzer commented on Apr 22, 2020

    @anntzer
    Contributor

    Ah, very good point, I guess we could just exclude them from tightbbox computation (but only in the "irrelevant" direction).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions