3

I'm pretty sure this is occurring in the portion of my code where I go to plot text:

xlows = x[local_min]; xhighs = x[local_max]
ylows = y[local_min]; yhighs = y[local_max]
lowvals = prmsl[local_min]; highvals = prmsl[local_max]
# plot lows as blue L's, with min pressure value underneath.
xyplotted = []
# don't plot if there is already a L or H within dmin meters.
yoffset = 0.022*(m.ymax-m.ymin)
dmin = yoffset
for x,y,p in zip(xlows, ylows, lowvals):
    if x < m.xmax and x > m.xmin and y < m.ymax and y > m.ymin:
        dist = [np.sqrt((x-x0)**2+(y-y0)**2) for x0,y0 in xyplotted]
        if not dist or min(dist) > dmin:
            plt.text(x,y,'L',fontsize=14,fontweight='bold',
                    ha='center',va='center',color='b')
            plt.text(x,y-yoffset,repr(int(p)),fontsize=9,
                    ha='center',va='top',color='b',
                    bbox = dict(boxstyle="square",ec='None',fc=(1,1,1,0.5)))
            xyplotted.append((x,y))

My source code is similar to this example third one down from the top.

The Traceback:

/Library/Python/2.7/site-packages/matplotlib/text.py:53: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  if rotation in ('horizontal', None):
/Library/Python/2.7/site-packages/matplotlib/text.py:55: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  elif rotation == 'vertical':

I did a print of the rotation values:

None
None
None
None
None
None
None
None
32.5360682877
/Library/Python/2.7/site-packages/matplotlib/text.py:53: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  if rotation in ('horizontal', None):
/Library/Python/2.7/site-packages/matplotlib/text.py:55: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  elif rotation == 'vertical':
32.5360682877
25.1125465842
25.1125465842
2.90036159155
2.90036159155
43.6364736689
43.6364736689

I'm not sure why this error is occurring.

wxcoder
  • 665
  • 1
  • 13
  • 32
  • Possible duplicate of [Python unicode equal comparison failed](https://stackoverflow.com/questions/18193305/python-unicode-equal-comparison-failed) – Alastair Irvine Jul 26 '17 at 10:12

1 Answers1

9

I can't say from your example exactly what is wrong, but this error occurs in Python 2.X when comparing a Unicode string to a byte string. Python 2.X attempts to implicitly convert the byte string to Unicode using the default ascii codec. If that fails, due to the byte string containing non-ASCII bytes, that warning occurs:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> u'pingüino' == 'pingüino'
__main__:1: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as
being unequal
False

Python 3.X reduces the confusion by not allowing non-ASCII characters in a Unicode string literal:

Python 3.3.5 (v3.3.5:62cf4e77f785, Mar  9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 'pingüino' == b'pingüino'
  File "<stdin>", line 1
SyntaxError: bytes can only contain ASCII literal characters.

Instead the programmer must be more explicit, comparing bytes to bytes or Unicode to Unicode, or providing the appropriate conversion:

>>> 'pingüino' == b'ping\xfcino'.decode('latin1')
True
>>> 'pingüino'.encode('latin1') == b'ping\xfcino'
True
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251