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

Continued Text Alignment Issues #1742

Closed
pmarshwx opened this issue Feb 9, 2013 · 12 comments
Closed

Continued Text Alignment Issues #1742

pmarshwx opened this issue Feb 9, 2013 · 12 comments
Assignees
Milestone

Comments

@pmarshwx
Copy link

pmarshwx commented Feb 9, 2013

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

@WeatherGod
Copy link
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.

@leejjoon
Copy link
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.

@mdboom
Copy link
Member

mdboom commented Feb 12, 2013

@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.)

@pmarshwx
Copy link
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.

@leejjoon
Copy link
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()

@ghost ghost assigned leejjoon Feb 12, 2013
@mdboom
Copy link
Member

mdboom commented May 21, 2013

@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.

@mdboom
Copy link
Member

mdboom commented May 28, 2013

@leejjoon: Do you agree with my suggestion above?

@leejjoon
Copy link
Contributor

Yes, that sounds reasonable.

@dmcdougall
Copy link
Member

Was this warning message ever merged?

@mdboom
Copy link
Member

mdboom commented Jun 24, 2013

@dmcdougall: No.

@dmcdougall
Copy link
Member

See #2166.

@dmcdougall
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants