将度数符号插入 python 图中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19926246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:59:26  来源:igfitidea点击:

Inserting a degree symbol into python plot

pythonmatplotlibsymbols

提问by user2739143

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.

这是一个非常简单的问题,但它逃脱了我。我只是想在我的 python 情节的标题和图例中插入一个度数符号。代码如下。谢谢。

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()

采纳答案by Mailerdaimon

Use LaTeX Style. For Example: $^\circ$ Textwould produce °Text

使用 LaTeX 样式。例如:$^\circ$ Text会产生°Text

See the matplotlib documentationfor more information about printing (especially mathematical expression).

有关打印(尤其是数学表达式)的更多信息,请参阅matplotlib 文档

In your case the code has to be: plt.xlabel('Manufactured Ply Angle $^\circ$')

在您的情况下,代码必须是: plt.xlabel('Manufactured Ply Angle $^\circ$')

The TeX part of the expression must be enclosed by dollar signs "$".

表达式的 TeX 部分必须用美元符号“$”括起来。

回答by dadaist

Use LaTeX math. On my system the best visual appearance is achieved with

使用 LaTeX 数学。在我的系统上,最好的视觉外观是通过

label = r'$45\degree$'

label = r'$45\degree$'

and it looks exactly like the default theta labels of a polar plot.

它看起来与极坐标图的默认 theta 标签完全一样。

As others have pointed out kludges like

正如其他人指出的那样

  • label = r'$45^\circ$'
  • label = '$45^o$'
  • 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.

等也能工作,但视觉外观不太好。在我的系统上,这些变通方法呈现的符号略小。YMMV,因此人们可能想尝试在她的系统上看起来最好的东西。

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))