1

I am looking for this character: Δ which I need for a legend item in matplotlib. Python 3.x features a str type that contains Unicode characters, but I couldn't find any valuable information about how to do it in Python 2.7.

x = range(10)
y = [5] * 10
z = [y[i] - x[i] for i in xrange(10)]

plt.plot(x,z,label='Δ x,y')
plt.legend()
plt.show()

UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 0: ordinal not in range(128)

offeltoffel
  • 2,691
  • 2
  • 21
  • 35
  • 1
    See the answer with the most votes [here](http://stackoverflow.com/questions/13338550/typing-greek-letters-etc-in-python-plots): `label=r'$\Delta$x,y'` – berna1111 Nov 15 '16 at 14:20
  • 1
    Thanks berna! I already found that answer, but when I try it, I get an error (German): "The command "latex" is either mis-spelled or could not be found" (possibly an error from the command shell?). And later (English): "RuntimeError: LaTeX was not able to process the following string: 'lp' " – offeltoffel Nov 15 '16 at 14:59
  • Yes, you would need latex installed for this shortcut to work. But you can see how to pick a new font with the symbol on the question I pointed (or the great answer by @Diziet ). – berna1111 Nov 15 '16 at 22:54

1 Answers1

4

Although @berna1111's comment is correct, you don't need to use LaTeX format to get a ∆ character. In python 2, you need to specify that a string is unicode by using the u'' construct (see doc here). E.g.:

plt.plot(x,z,label=u'Δ x,y')
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • Thanks, Diziet! Your suggestion suppresses the error, but the symbol cannot be displayed in the plot. There is just the blank rectangle. – offeltoffel Nov 15 '16 at 14:55
  • 2
    you probably use a font that does not have the ∆ character. Try to play around with fonts in rcParams (see http://matplotlib.org/users/customizing.html) or try something like `plt.legend(prop={'family':'Times New Roman'})` – Diziet Asahi Nov 15 '16 at 15:10