2

Below are some pieces of code, but I really do not know how to let ℃ shows as it should be. I think matplotlib is powerful enough to do tihs kind of job. Any experts can help me out of this dilemma?

if i==12:
    plot(strain,stress,'1-',label="$1200℃$")

xlabel(r"strain/$\epsilon$",fontsize=18)
ylabel("stress/MPa",fontsize=18)

legend(bbox_to_anchor=(1.01, 1.0,0.8,0.0), loc=2, borderaxespad=0.0)

show()
hitzg
  • 12,133
  • 52
  • 54
user3737702
  • 591
  • 1
  • 10
  • 17
  • 1
    possible duplicate of [Non-ASCII characters in Matplotlib](http://stackoverflow.com/questions/10960463/non-ascii-characters-in-matplotlib) – Gerrat Aug 22 '14 at 02:34
  • MathTextWarning: Font 'rm' does not have a glyph for 'b'\\u2103''[U2103]. This is warning from python, I have read the links you gave to me, however, still have no idea how to fix it. I have tried using" # -*- coding: utf-8 -*-", but it does not help. – user3737702 Aug 22 '14 at 02:51
  • Font 'rm'?? Did you set it via: `matplotlib.rc('font', family='Arial')` ? – Gerrat Aug 22 '14 at 02:53
  • No, I did not do that. Actually this warning comes from ℃, because after I deleted the ℃, there is no warning. However without ℃, the figure will be less meaningful, since nobody knows the meaning of the legend. – user3737702 Aug 22 '14 at 02:58
  • Well, that was the most relevant part of the link I posted. – Gerrat Aug 22 '14 at 03:00
  • 1
    You should try *all* the solutions discussed in the linked answer. You are trying to display a unicode character (℃), which the default font does not include. You may also need to prefix your string with `u''`, if you are using Python 2. – Marius Aug 22 '14 at 03:01
  • Thank you for your patient guide and reply, Marius. :) – user3737702 Aug 22 '14 at 03:03

1 Answers1

4

This solution is completely separate to the font/unicode issues discussed in the duplicate, but you can use Latex-style commands to get this working:

plt.plot(1, 3, '1-', label=u"$1200^{\circ}C$")
plt.legend()
plt.show()
Marius
  • 58,213
  • 16
  • 107
  • 105
  • Good lord, Thank you for your solution. You are really good at matplotlib. My level is too low, still need to learn more...Latex-style pretty cool!! – user3737702 Aug 22 '14 at 03:01
  • This solution is not really so much about having deep matplotlib knowledge, it's more about being familiar with Latex's math formatting commands, which matplotlib happens to (partly) support. – Marius Aug 22 '14 at 03:03
  • Nice - even simpler alternative. – Gerrat Aug 22 '14 at 03:04
  • Yeah, I knew matplotlib support Latex formatting. Still, I do not dive so deep into it so far as you do. It really helps~ – user3737702 Aug 22 '14 at 03:06