[英]How do I print a Celsius symbol with matplotlib?
我想打印一個軸標簽:“溫度(℃)”。 我該怎么做? 一個片段是這樣的:
# -*- 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 (℃)')
對於最后一行,我嘗試過:
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)')
我只是不明白。 我正在使用spyder並從那里運行代碼。
使用LaTeX解釋器制作度數符號。
ax.set_xlabel('Temperature ($^\circ$C)')
結果如下:
ax.set_xlabel(u'Temperature (℃)')
應該管用:
In [56]: matplotlib.__version__
Out[56]: '1.0.1'
而不是DEGREE CELSIUS U + 2103(℃),使用DEGREE SIGN U + 00B0(°)后跟大寫字母。 由於幾個原因,包括字體覆蓋,這更安全。 它也是Unicode標准中推薦的方式(15.2字母符號;第481頁)。
要在沒有LaTex解釋器的情況下在matplotlib中使用它,請使用unicode格式化和 unicode字符串
from numpy import arange, cos, pi
from matplotlib.pyplot import (figure, axes, plot, xlabel, ylabel, title,
grid, show)
figure(1, figsize=(6,4))
ax = axes([0.1, 0.1, 0.8, 0.7])
t = arange(0.0, 1.0 + 0.01, 0.01)
s = 3*cos(2*pi*t)+25
plot(t, s)
title('Average High Temperature')
xlabel('Year')
ylabel(u'Temp (\u00B0C)')
grid(True)
show()
要么:
ax.set_xlabel(u'Temperature (\N{DEGREE SIGN}C)')
如果你想讓它與TeX 和非TeX兼容,那么可能必須使用兩種方法並在之前測試if rcParams['text.usetex']
。 例如,這是在底圖中完成的 。
Spyder用戶的更新:
$ ^ \\ circ $ - 有效!
\\ N {DEGREE SIGN} - 給我一個小寫的伽瑪符號......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.