Inserting a degree symbol into python plot
Question:
This is a really simple problem but its escaping me. I’m just trying to insert a degree symbol into the titles and legends of my python plot. Code is below. Thanks.
from numpy import *
import numpy as np
import matplotlib.pyplot as plt
theta1 = linspace(0,60,610)
theta2 = linspace(0,45,460)
theta3 = linspace(45,90,460)
CTS = 1/cos(radians(theta1))
CTS0 = 1/cos(radians(60-theta2))
CTS45 = 1/cos(radians(105-theta3))
plt.plot(theta1,CTS,label=u'CTS Head at 0',linewidth=2)
plt.plot(theta2,CTS0,label='CTS Head at 60',linewidth=2)
plt.plot(theta3,CTS45,label='CTS Head at 105',linewidth=2)
plt.xlabel('Manufactured Ply Angle (degrees)')
plt.ylabel('Thickness')
plt.legend( loc='lower right', numpoints = 1 )
plt.ylim([0,2.5])
plt.grid(b=None, which='major', axis='both')
plt.grid(color='k', linestyle='--', linewidth=0.5)
plt.axhline(y=1.035, xmin=0, xmax=90,color='k', linestyle='-', linewidth=1)
plt.show()
Answers:
Use LaTeX Style. For Example: $^circ$ Text
would produce °Text
See the matplotlib documentation for more information about printing (especially mathematical expression).
In your case the code has to be: plt.xlabel('Manufactured Ply Angle $^circ$')
The TeX part of the expression must be enclosed by dollar signs “$”.
Use LaTeX math.
On my system the best visual appearance is achieved with
label = r'$45degree$'
and it looks exactly like the default theta labels of a polar plot.
As others have pointed out kludges like
label = r'$45^circ$'
label = '$45^o$'
etc. work too but the visual appearance is not so good.
On my system these workarounds render a symbol that is slightly too small.
YMMV, thus one may want to try what looks best on her system.
For example on a polar contour plot where radius is sine of zenith angle
one may want to use
deg_labels = np.array([5, 10, 20, 30, 45, 60, 90])
ax.set_rgrids(np.sin(np.deg2rad(deg_labels)),
labels=(r"${:.0f}degree$".format(_) for _ in deg_labels))
Use degree unicode symbol, especially if you don’t need LaTeX for other symbols.
U+00B0: °
In python3, it’s just: plt.xlabel("Manufactured Ply Angle (°)")
Similar to the answer given by dadaist, this also works for me to generate a degree symbol:
N{degree sign}
so, in your example it would be
plt.xlabel('Manufactured Ply Angle (N{degree sign})')
This is a really simple problem but its escaping me. I’m just trying to insert a degree symbol into the titles and legends of my python plot. Code is below. Thanks.
from numpy import *
import numpy as np
import matplotlib.pyplot as plt
theta1 = linspace(0,60,610)
theta2 = linspace(0,45,460)
theta3 = linspace(45,90,460)
CTS = 1/cos(radians(theta1))
CTS0 = 1/cos(radians(60-theta2))
CTS45 = 1/cos(radians(105-theta3))
plt.plot(theta1,CTS,label=u'CTS Head at 0',linewidth=2)
plt.plot(theta2,CTS0,label='CTS Head at 60',linewidth=2)
plt.plot(theta3,CTS45,label='CTS Head at 105',linewidth=2)
plt.xlabel('Manufactured Ply Angle (degrees)')
plt.ylabel('Thickness')
plt.legend( loc='lower right', numpoints = 1 )
plt.ylim([0,2.5])
plt.grid(b=None, which='major', axis='both')
plt.grid(color='k', linestyle='--', linewidth=0.5)
plt.axhline(y=1.035, xmin=0, xmax=90,color='k', linestyle='-', linewidth=1)
plt.show()
Use LaTeX Style. For Example: $^circ$ Text
would produce °Text
See the matplotlib documentation for more information about printing (especially mathematical expression).
In your case the code has to be: plt.xlabel('Manufactured Ply Angle $^circ$')
The TeX part of the expression must be enclosed by dollar signs “$”.
Use LaTeX math.
On my system the best visual appearance is achieved with
label = r'$45degree$'
and it looks exactly like the default theta labels of a polar plot.
As others have pointed out kludges like
label = r'$45^circ$'
label = '$45^o$'
etc. work too but the visual appearance is not so good.
On my system these workarounds render a symbol that is slightly too small.
YMMV, thus one may want to try what looks best on her system.
For example on a polar contour plot where radius is sine of zenith angle
one may want to use
deg_labels = np.array([5, 10, 20, 30, 45, 60, 90])
ax.set_rgrids(np.sin(np.deg2rad(deg_labels)),
labels=(r"${:.0f}degree$".format(_) for _ in deg_labels))
Use degree unicode symbol, especially if you don’t need LaTeX for other symbols.
U+00B0: °
In python3, it’s just: plt.xlabel("Manufactured Ply Angle (°)")
Similar to the answer given by dadaist, this also works for me to generate a degree symbol:
N{degree sign}
so, in your example it would be
plt.xlabel('Manufactured Ply Angle (N{degree sign})')