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
Conversation
@@ -675,11 +675,9 @@ def set_label(self, s): | |||
|
|||
ACCEPTS: any string | |||
""" | |||
self._label = s | |||
self._label = '%s' % (s, ) |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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'
How about I add a unicode example to see what happens! |
@@ -675,11 +675,9 @@ def set_label(self, s): | |||
|
|||
ACCEPTS: any string |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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 ;)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Other than my comment about using escape sequences, this seems good to go. |
Thanks @efiring. That was very helpful - I definitely need to read up about best practices wrt to unicode handling. |
Fixes #775.
I did consider only testsing using
png
, since the code being tested is backend agnostic. Any thoughts?