Skip to content

Continued Text Alignment Issues #1742

Closed

Description

@pmarshwx

When using AnchoredText, setting the horizontal alignment to 'center' causes the text to be centered around the left-most edge of the text space. In other words, the text is being centered around the location of the the left spine of the text box plus the padding amount.

If you try to right-align the text, the entire text is actually found to the left of the text box.

Here's a simple example and figure to illustrate. Note, this works with or without ImageGrid.

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText
fig = plt.figure()
# grid = ImageGrid(fig, 111, nrows_ncols=(1,1))
# ax0 = grid[0]
ax0 = fig.add_subplot(111)
title = 'Test Test Test Test Test'
at = AnchoredText(title, prop=dict(size=24, horizontalalignment='center'),
                  frameon=True, loc=9, borderpad=0.5, pad=0.5)
at.patch.set_boxstyle("round, pad=0, rounding_size=0.25")
ax0.add_artist(at)
plt.show()

tmp

Here are a couple of issues and pull requests that could be related.

Issue #1571
Pull Request #1081
Pull Request #1589

Activity

  1. WeatherGod commented on Feb 11, 2013

    @WeatherGod
    Member

    It looks like the offset for the AnchoredOffsetbox (which AnchoredText is subclassed from) is only applied to the patch object, never the text object. Furthermore, the Anchor's offset is calculated only when the frame is made to be visible. I vaguely remember this codepath being tinkered with fairly recently with respect to legend placement. Maybe we borked something then?

    The relevant codepath looks to be the same for both master and v1.2.x branches, so I would say that this might be a regression for v1.2.x.

  2. leejjoon commented on Feb 12, 2013

    @leejjoon
    Contributor

    Although it is not clearly documented, Changing the alignments of the text inside the AnchoredText (or anything derived from TextArea) is not supported. We may need some warning here though.

    If you want to adjust the position of the AnchoredText, you need to fiddle with parameters like loc, bbox_to_anchor, or bbox_transform. While there is subtle difference, the legend location section of the legend guide (http://matplotlib.org/users/legend_guide.html) may be useful.

  3. mdboom commented on Feb 12, 2013

    @mdboom
    Member

    @leejjoon: Since I've never used the AnchoredText stuff, can you clarify what isn't supported? In the example, it looks like the horizontalalignment of the text is set on creation of the AnchoredText object, not changed later. If it's possible to add a warning in this case, I think that would be great. (There are actually lots of places around matplotlib where things can't be changed after creation time where warnings might be helpful -- or to just fix it so it works where possible or easy to do.)

  4. pmarshwx commented on Feb 12, 2013

    @pmarshwx
    Author

    Can horizontal alignment be supported here? Is this something that can eventually be added? Or is it extremely complicated?

    The reason why I think this is important is that AnchoredText is extremely powerful for including captions/titles inside a figure vs. using ax.set_title(). (This is how I use AnchoredText a majority of the time.) It's much easier than trying to figure out the correct placements when using basemap (which can alter the shape of the viewing window). It doesn't really matter when the title is a single line, but when you move to multiple lines, being able to center is useful.

  5. leejjoon commented on Feb 12, 2013

    @leejjoon
    Contributor

    @mdboom, the placement of AnchoredText is similar to the legend, i.e., the position of the text is determined by the loc parameter and the bounding box that it is anchored to. And such placement conflicts with setting the alignments of the text (ha & va). Implementation-wise, the placement of AnchoredText is done using left and baseline alignment. I will try to add a warning message.

    @pmarshwx, Now I see your problem, you meant alignments of multiple lines. I must confess that the multiline support of the current AnchoredText implementation is not good enough. This is something that need to be improved I guess. Meanwhile, here is a workaround, although more complicated.

    import matplotlib.pyplot as plt
    from matplotlib.offsetbox import TextArea, VPacker, AnchoredOffsetbox
    
    fig = plt.figure()
    ax0 = fig.add_subplot(111)
    
    prop=dict(size=24)
    
    t1 = TextArea("Test Test", textprops=prop)
    t2 = TextArea("Test", textprops=prop, minimumdescent=False)
    offsetbox = VPacker(children=[t1, t2], align="center", pad=0, sep=4)
    
    at = AnchoredOffsetbox(loc=9, child=offsetbox, bbox_to_anchor=ax0.bbox)
    
    ax0.add_artist(at)
    
    at.patch.set_boxstyle("round, pad=0, rounding_size=0.25")
    plt.show()
  6. mdboom commented on May 21, 2013

    @mdboom
    Member

    @leejjoon: So I think for 1.3.x, all we'll want to do with this is add a warning message when AnchoredText is mixed with ha and va. Is that correct? We can try to fix some of the deeper issues later.

  7. mdboom commented on May 28, 2013

    @mdboom
    Member

    @leejjoon: Do you agree with my suggestion above?

  8. leejjoon commented on May 29, 2013

    @leejjoon
    Contributor

    Yes, that sounds reasonable.

  9. dmcdougall commented on Jun 23, 2013

    @dmcdougall
    Member

    Was this warning message ever merged?

  10. mdboom commented on Jun 24, 2013

    @mdboom
    Member
  11. dmcdougall commented on Jun 28, 2013

    @dmcdougall
    Member

    See #2166.

  12. dmcdougall commented on Jul 1, 2013

    @dmcdougall
    Member

    #2166 was merged so I'm closing this.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions