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

Added %s support for labels. #1107

Merged
merged 4 commits into from Aug 21, 2012
Merged

Added %s support for labels. #1107

merged 4 commits into from Aug 21, 2012

Conversation

pelson
Copy link
Member

@pelson pelson commented Aug 19, 2012

Fixes #775.

I did consider only testsing using png, since the code being tested is backend agnostic. Any thoughts?

@@ -675,11 +675,9 @@ def set_label(self, s):

ACCEPTS: any string
"""
self._label = s
self._label = '%s' % (s, )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this equivalent to str(s)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently not (I always thought it was until this evening):

>>> a = u"Hello\u2026"
>>> str(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 5: ordinal not in range(128)
>>> '%s' % a
u'Hello\u2026'

@pelson
Copy link
Member Author

pelson commented Aug 19, 2012

How about I add a unicode example to see what happens!

@@ -675,11 +675,9 @@ def set_label(self, s):

ACCEPTS: any string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the change below, it looks like set_label accepts virtually any python object! So, perhaps change the docstring to reflect that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a tricky one! The text.py module, which does the same (and is where I stole the idea from) states:

ACCEPTS: string or anything printable with '%s' conversion.

How does that sound?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works.

ax = fig.add_subplot(121)
x = np.arange(100)
ax.plot(range(4), 'o', label=1)
ax.plot(np.linspace(4, 4.1), 'o', label=u'Développés')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this ever made it into the code guidelines, but we've tried not to have unicode-encoded files like this, but instead use \u escape sequences for unicode characters. I know it's not as obvious -- but some developers are still using editors that don't handle unicode encodings correctly -- I'm not one of them ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you might mention this. :-)
My unicode knowledge is weak at best, but I will try to figure out a suitable unicode string which can go in the label.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 2012/08/20 3:35 AM, Michael Droettboom wrote:

I don't know if this ever made it into the code guidelines, but we've
tried not to have unicode-encoded files like this, but instead use |\u|
escape sequences for unicode characters. I know it's not as obvious --
but some developers are still using editors that don't handle unicode
encodings correctly -- I'm not one of them ;)

Thank you. I am one of them, maybe the only one.

This may help: http://www.utf8-chartable.de

One incredibly verbose but effective mechanism for including unicode
looks like this:

u'D\N{LATIN SMALL LETTER E WITH ACUTE}velopp\N{LATIN SMALL LETTER E WITH
ACUTE}s'

String substitution with %s can be used to make it shorter.

Or one can use this:
u'D\xe9velopp\xe9s'

again with the option of string substitution to make it a bit clearer.

Eric

@mdboom
Copy link
Member

mdboom commented Aug 20, 2012

Other than my comment about using escape sequences, this seems good to go.

@pelson
Copy link
Member Author

pelson commented Aug 20, 2012

Thanks @efiring. That was very helpful - I definitely need to read up about best practices wrt to unicode handling.
This should now be good to merge.

mdboom added a commit that referenced this pull request Aug 21, 2012
@mdboom mdboom merged commit 22e6070 into matplotlib:master Aug 21, 2012
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 this pull request may close these issues.

TypeError in Axes.get_legend_handles_labels
3 participants