4

I've been trying to put the astronomical symbol of the sun on a graph using PyX, but so for with no success. The code I have is the following:

 from pyx import *
 from pylab import *
 x=arange(1,5,0.1)
 y=exp(-(x-3.0)**2/(2.0*0.5**2))/sqrt(2.0*pi*0.5**2)
 ######################
 g=graph.graphxy(width=8,y=graph.axis.linear(title=r"Fraction of DM halos"),x=graph.axis.linear(min=1,title=r"Mass ($10^{11}M_{\sun}$)"))
 g.plot(graph.data.values(x=x,y=y),styles=[graph.style.histogram()])
 g.writeEPSfile("testhistogram")

I tried adding text.set(mode="latex") followed by text.preamble("\usepackage{mathabx}"), but this doesn't work (because I know this symbol is on the mathabx LaTeX package). Any ideas?

woot
  • 7,406
  • 2
  • 36
  • 55
Néstor
  • 585
  • 8
  • 20

1 Answers1

4

I am without pyx here, but have you simply tried to use Unicode strings and pass in the unicode character for the symbol you want?

The char for the sun symbol has unicode number 9737 (decimal, 0x2609 hex) , so you could just try doing this:

g=graph.graphxy(width=8,y=graph.axis.linear(title=r"Fraction of DM halos"),
   x=graph.axis.linear(min=1,
       title=u"Mass ($10^{11}M_\u2609$)"))
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • It doesn't work :-(. The error is: 'UnicodeEncodeError: 'ascii' codec can't encode character u'\u2609' in position 177: ordinal not in range(128)'. Furthermore, would this be the only way? I think I'll have a hard time memorizing all the unicode characters... – Néstor Sep 19 '12 at 01:45
  • Read http://www.joelonsoftware.com/articles/Unicode.html to understand text how it is dealt in afeter 1990. Seriously - "unicode" is not that monster - it is a must know. You probably missed the `u" ` perfixing., And of course no one memorizes all unicode points: you use an app, or a table for that, or just print then on Python console with something like `>>> for i in range(32, 20000): print u"%d - %s, " %(i, unichr(i)), – jsbueno Sep 24 '12 at 02:12
  • On the other hand, if you _did_ put the `u` prefixing the `"`, it means the library you are using is treating text as ASCII only. Read the article I linked above to understand it is a bug in the library, and report the issue. And wait for someone to provide you with another answer in the meantime. People who make standards about how to deal with ext in computers had worried about adding astronomical symbols alrady so that other people should not have problems doing this. – jsbueno Sep 24 '12 at 02:16