Search code examples
pythonunicodelatexmatplotlib

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.


Solution

  • Use the LaTeX interpreter to make the degree symbol.

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

    Here's the results:

    enter image description here