How do I write a capital Greek “delta” as a string in Python 2.7?

安稳与你 提交于 2019-12-12 02:40:05

问题


I am looking for this character: Δ which I need for a legend item in matplotlib. Python 3.x features a str type that contains Unicode characters, but I couldn't find any valuable information about how to do it in Python 2.7.

x = range(10)
y = [5] * 10
z = [y[i] - x[i] for i in xrange(10)]

plt.plot(x,z,label='Δ x,y')
plt.legend()
plt.show()

UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 0: ordinal not in range(128)


回答1:


Although @berna1111's comment is correct, you don't need to use LaTeX format to get a ∆ character. In python 2, you need to specify that a string is unicode by using the u'' construct (see doc here). E.g.:

plt.plot(x,z,label=u'Δ x,y')


来源:https://stackoverflow.com/questions/40611410/how-do-i-write-a-capital-greek-delta-as-a-string-in-python-2-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!