Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Matplotlib – How to insert a degree symbol into a Python plot?
To insert a degree symbol into a Python plot, you can use LaTeX representation with the syntax $^\circ$. This is particularly useful when plotting temperature data or angular measurements.
Steps
- Create data points for pV, nR and T using numpy
- Plot pV and T using
plot()method - Set xlabel for pV using
xlabel()method - Set the label for temperature with degree symbol using
ylabel()method - To display the figure, use
show()method
Example
Let's create a simple temperature vs pressure×volume plot with a degree symbol in the y-axis label ?
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
pV = np.array([3, 5, 1, 7, 10, 9, 4, 2])
nR = np.array([31, 15, 11, 51, 12, 71, 41, 13])
T = np.divide(pV, nR)
plt.plot(pV, T, c="red")
plt.xlabel("Pressure x Volume")
plt.ylabel("Temperature ($^\circ$C)")
plt.show()
Output
The above code will display a line plot with the y-axis labeled as "Temperature (°C)" where the degree symbol is properly rendered.
Alternative Methods
You can also use Unicode character or raw string formatting ?
import matplotlib.pyplot as plt
import numpy as np
# Method 1: Unicode character
plt.ylabel("Temperature (\u00B0C)")
# Method 2: Raw string with LaTeX
plt.ylabel(r"Temperature ($^\circ$C)")
# Method 3: For angles in degrees
plt.xlabel("Angle ($^\circ$)")
plt.show()
Key Points
-
$^\circ$− LaTeX representation (most common) -
\u00B0− Unicode character for degree symbol -
r"..."− Raw string prevents escape character issues - Works in titles, axis labels, and text annotations
Conclusion
The LaTeX representation $^\circ$ is the most reliable method to insert degree symbols in Matplotlib plots. Use raw strings with r"" or Unicode \u00B0 as alternatives when needed.
Advertisements
