How do I print a Celsius symbol with Matplotlib?

To print a Celsius symbol (°C) with Matplotlib, you can use LaTeX mathematical notation within text labels. The degree symbol is rendered using ^\circ in math mode.

Basic Example with Celsius Symbol

Here's how to display the Celsius symbol in axis labels ?

import matplotlib.pyplot as plt
import numpy as np

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Generate sample temperature data
N = 10
temperature = np.random.rand(N) * 30  # Temperature in Celsius
pressure = np.random.rand(N) * 100    # Pressure values

# Create the plot
plt.plot(temperature, pressure, 'bo-')
plt.xlabel("Temperature $^\circ$C")
plt.ylabel("Pressure (kPa)")
plt.title("Temperature vs Pressure")

plt.show()

Different Methods to Display Celsius Symbol

Method 1: Using LaTeX Math Mode

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 100, 50)
y = x * 2 + np.random.normal(0, 10, 50)

plt.plot(x, y, 'r-')
plt.xlabel("Temperature ($^\circ$C)")
plt.ylabel("Heat Output (W)")
plt.show()

Method 2: Using Unicode Degree Symbol

import matplotlib.pyplot as plt
import numpy as np

temps = [20, 25, 30, 35, 40]
efficiency = [0.8, 0.82, 0.85, 0.83, 0.81]

plt.bar(temps, efficiency)
plt.xlabel("Temperature (°C)")  # Unicode degree symbol
plt.ylabel("Efficiency")
plt.title("Engine Efficiency vs Temperature")
plt.show()

Advanced Formatting with Celsius Symbol

You can combine the Celsius symbol with other formatting for more complex labels ?

import matplotlib.pyplot as plt
import numpy as np

time = np.arange(0, 24, 1)
temp = 20 + 10 * np.sin(np.pi * time / 12)

plt.plot(time, temp, 'g-', linewidth=2)
plt.xlabel("Time (hours)")
plt.ylabel("Temperature ($^\circ$C)")
plt.title("Daily Temperature Variation")

# Add text annotation with Celsius symbol
plt.annotate(f'Max: {max(temp):.1f}$^\circ$C', 
             xy=(12, max(temp)), 
             xytext=(15, max(temp)+2),
             arrowprops=dict(arrowstyle='->'))

plt.grid(True, alpha=0.3)
plt.show()

Comparison of Methods

Method Syntax Pros Cons
LaTeX Math Mode $^\circ$C Consistent rendering Requires LaTeX knowledge
Unicode Symbol °C Simple and direct May vary across systems
Raw String r'$^\circ$C' Handles escape characters More verbose

Conclusion

Use $^\circ$C in LaTeX math mode for consistent Celsius symbol rendering in Matplotlib. For simple cases, the Unicode degree symbol °C works well and is more readable in code.

Updated on: 2026-03-25T22:42:49+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements