Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print a Celsius symbol with matplotlib?

I want to print an axis label: "Temperature (℃)". How do I do it? A snippet is this:

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
x = range(10,60,1)
y = range(-100, 0, 2)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
ax.set_xlabel('Temperature (℃)')

For that last line I have tried:

ax.set_xlabel('Temperature (℃)'.encode('utf-8'))

ax.set_xlabel(u'Temperature (u\2103)')

ax.set_xlabel(u'Temperature (℃)')

ax.set_xlabel(u'Temperature (\u2103)')

ax.set_xlabel('Temperature (\u2103)')

I just don't get it. I'm using spyder and running the code from there.

like image 382
a different ben Avatar asked Dec 09 '11 01:12

a different ben


People also ask

How do I print values in matplotlib?

The key to print the values is the string function str(), that we used with the annotate function of the matplotlib.


Video Answer


5 Answers

Use the LaTeX interpreter to make the degree symbol.

ax.set_xlabel('Temperature ($^\circ$C)')

Here's the results:

enter image description here

like image 80
Carl F. Avatar answered Oct 24 '22 06:10

Carl F.


ax.set_xlabel(u'Temperature (℃)')

should work:

enter image description here

In [56]: matplotlib.__version__
Out[56]: '1.0.1'
like image 43
unutbu Avatar answered Oct 24 '22 07:10

unutbu


Instead of DEGREE CELSIUS U+2103 (℃), use the DEGREE SIGN U+00B0 (°) followed by the capital letter. This is much safer for several reasons, including font coverage. It is also the way recommended in the Unicode Standard (15.2 Letterlike symbols; p. 481).

like image 25
Jukka K. Korpela Avatar answered Oct 24 '22 08:10

Jukka K. Korpela


To make this work in matplotlib without the LaTex interpreter, use unicode formatting AND the unicode character string

from numpy import arange, cos, pi
from matplotlib.pyplot import (figure, axes, plot, xlabel, ylabel, title,
                               grid, show)
figure(1, figsize=(6,4))
ax = axes([0.1, 0.1, 0.8, 0.7])
t = arange(0.0, 1.0 + 0.01, 0.01)
s = 3*cos(2*pi*t)+25
plot(t, s)

title('Average High Temperature')
xlabel('Year')
ylabel(u'Temp (\u00B0C)')
grid(True)
show()

enter image description here

like image 42
Jesuisme Avatar answered Oct 24 '22 08:10

Jesuisme


Or:

ax.set_xlabel(u'Temperature (\N{DEGREE SIGN}C)')

If you want to make it compatible to TeX and non-TeX, then one probably has to use both ways and test with if rcParams['text.usetex'] before-hand. This is how it's done in basemap for example.

like image 39
letmaik Avatar answered Oct 24 '22 08:10

letmaik