Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Layout managers have problems with titles containing MathText #11468

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

Closed
ImportanceOfBeingErnest opened this issue Jun 20, 2018 · 8 comments · Fixed by #11499
Closed

Layout managers have problems with titles containing MathText #11468

ImportanceOfBeingErnest opened this issue Jun 20, 2018 · 8 comments · Fixed by #11499
Milestone

Comments

@ImportanceOfBeingErnest
Copy link
Member

Layout managers seem to have problems with titles containing MathText.

One would expect to have a title which neither overlaps with the axes nor with the figure boundary in all cases depicted below.

tight_layout

multi line title

tight_layout2

as expected

multi line title with MathText

tight_layout1

The title is a bit squeezed towards the top figure boundary, but does not go beyond it, nor does it overlap the axes. This is somehow still acceptable.

single line title with MathText

tight_layout0

Here the title overlaps with the axes. This is in thus far strange because in the case above with multiple lines, this overlap does not occur.

constrained_layout

multi line title

constrained_layout2

as expected

multi line title with MathText

constrained_layout1

The title exits the figure. This is not desiredeable.

single line title with MathText

constrained_layout0

Here the title overlaps with the axes.

code

import matplotlib
import matplotlib.pyplot as plt
plt.rcParams['savefig.facecolor']='#e9f7fd'

def create_bug(title="", constrained_layout=True, i=0):
    plt.figure(constrained_layout=constrained_layout)
    plt.plot([0,1,2],[0,1,1])
    plt.title(title)
    
    if not constrained_layout:
        plt.tight_layout()
        tx = "tight_layout"
    else:
        tx = "constrained_layout"
    plt.text(0.4,.95, tx, va="center")
    plt.savefig(tx+str(i)+".png")
    
titles = ['line $\sum_{i} x_i$',
          'multi-\nline $\sum_{i} x_i$',
          'multi-\nline']
for layout in [True, False]:
    for i, title in enumerate(titles):
        create_bug(title, layout, i)

Matplotlib version

  • Operating system: Windows 8.1
  • Matplotlib version: 2.2.2, current master
  • Matplotlib backend : Qt5Agg
  • Python version: 2.7, 3.6
@jklymak
Copy link
Member

jklymak commented Jun 20, 2018

Thats because we have no way of knowing the text size other than by the fontsize. If we stick a huge symbol like $\sum_{i}$ in there, we don't know that its so much larger. I'm not sure what particularly can be done about this w/o a lot of work in the get_tightbbox code for the text.

@ImportanceOfBeingErnest
Copy link
Member Author

ImportanceOfBeingErnest commented Jun 22, 2018

Sure? If only fontsize matters, how do you find out the difference between "iiiiiiiiiii" and "mmmmmmmmmm", which clearly is accounted for, e.g. in the case of ylabels.

See e.g. this image.

image


Here are the boxes for the text obtained via get_window_extent() for the respective cases:

tight_layout

tight_layout2zz       tight_layout1zz       tight_layout0zz

constrained_layout

constrained_layout2zz      constrained_layout1zz      constrained_layout0zz

Seeing this it might be understandable that the multiline with mathtext (middle column) is cut on top. But what is really strange is that in the case of the single line (right column), the bounding box is actually correct, but still overlaps with the axes.

So in total those might be two different issues:

  • The bounding box (window extent) is wrong for such multiline mathtext.
  • The bounding box is ignored by the be layout managers.

@jklymak
Copy link
Member

jklymak commented Jun 22, 2018

OK, the last column the problem is that neither layout manager cares if the title overlaps the axes. Thats the fault of where the title is being placed. If you call w/ no layout manager the i still overlaps. The way the layout managers work is by adjusting the size of the axes assuming a border that takes up the other decorators; here the layout managers are doing the right thing from their point of view. If we wanted something that does a better job of positioning axes that could be done separately.

The middle column is definitely just a problem with the bbox from multiline w/ text. Multi lines are hard: #9775

@jklymak
Copy link
Member

jklymak commented Jun 22, 2018

Hmmm, though #9498 should have taken care of the right hand column case. I'll need to see whats going on there.

@jklymak
Copy link
Member

jklymak commented Jun 22, 2018

Ah... #9498 only does its thing if the xaxis is on top. An easy fix is to also move the title if it overlaps the actual axes. My only concern there is performance because I think the axes tightbbox calf can be expensive.

@jklymak
Copy link
Member

jklymak commented Jun 23, 2018

OK, not finding a nice consistent way to do this. OTOH, note that there is a pad argument now for set_title, so the user can simply specify a larger pad (in points) to get the title far enough away from the axes. #9816 and https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_title.html

@jklymak
Copy link
Member

jklymak commented Jul 8, 2018

@ImportanceOfBeingErnest please see #11499 and #11502. I think together they solve the issue here...

@ImportanceOfBeingErnest
Copy link
Member Author

Yep, it seems so. Once they are merged, this can be closed.

@QuLogic QuLogic added this to the v3.1 milestone Sep 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants