2

I am trying to type a text (which includes some astrophysical symbols like solar mass and Hubble's Parameter) inside an empty figure in a python script:

import numpy as np
import matplotlib.pyplot as plt

plt.figure(4)
frame = plt.gca()
frame.axes.get_xaxis().set_ticks([])
frame.axes.get_yaxis().set_ticks([])
A = 2
B = 2
C = 3
D = 4 
plt.text(0.05, 0.05, r'$R_{200m}$={:.0f} kpc physical \n\n $M_{200m}$={:.3e} $h^{-1} M_{\sun}$ \n\n\n\n x={:.0f} \n\n $M_{DM}$={:.3e} $h^{-1} M_{\odot}$'.format(A, B, C, D), size=20)
plt.show()

I am receiving the following error message after running the script with python3 example.py :

  File "exam.py", line 12, in <module>
      plt.text(0.05, 0.05, r'$R_{200m}$={:.0f} kpc physical \n\n $M_{200m}$={:.3e} $h^{-1} M_{\sun}$ \n\n\n\n x={:.0f} \n\n
  $M_{DM}$={:.3e} $h^{-1} M_{\odot}$'.format(A, B, C, D), size=20) 
  KeyError: '200m'

I don't know how to make LateX typings possible inside a python script?


Attached is the snapshot of the error message when running a code in which I am using the same format as the one suggested in the answer. I am running this code on an anaconda in which only python3.5 has been installed:

enter image description here

Rebel
  • 472
  • 8
  • 25

1 Answers1

2

replace your plt.text... line with the following:

plt.text(0.05, 0.05, '$R_{{200m}}$={:.0f} kpc physical \n\n $M_{{200m}}$={:.3e} $h^{{-1}} M_\u2609$ \n\n\n\n x={:.0f} \n\n $M_{{DM}}$={:.3e} $h^{{-1}} M_{{\odot}}$'.format(A, B, C, D), size=20)

I just:

  1. replaced {\sun} with \u2609, see How to do the astronomical symbol "\sun" in PyX.
  2. doubled the { of each LateX one, so the format method won't put its parameters in it.
  3. removed the r before the string so that the \ns will affect.

enter image description here

Community
  • 1
  • 1
Ophir Carmi
  • 2,701
  • 1
  • 23
  • 42
  • Thank You, that is interesting. – Rebel Sep 29 '16 at 19:19
  • Can I make the text $kpc physical$ in italic form? – Rebel Sep 29 '16 at 21:33
  • Thanks, Just figured that out: ${{kpc \/physical}}$ – Rebel Sep 30 '16 at 02:24
  • Hi Ophir, when running the same code under anaconda python 3.5, I am receiving the following error message: ValueError: h^{-1} M_\u2609 Expected {| ...|... |... |... |... |... |... |... |... |... |... |} (at char 10), (line: 1, col: 11) I am not sure if some packages are needed given that only python is installed for me by the IT person on a remote machine. – Rebel Nov 15 '16 at 06:51
  • I didn't understand if you have anaconda or just python – Ophir Carmi Nov 15 '16 at 08:20
  • I just have pure python packages on top of anaconda without extra packages that are needed but missing. I just want to know what are they that are causing such an error. – Rebel Nov 16 '16 at 20:32
  • I just added the full screen of most of the error message I am receiving in the line of code in which I am using the suggestions you made except that I am running the same code on a different machine in which only Python is installed. – Rebel Nov 16 '16 at 20:58
  • 1
    I get your error only if I use python 2.7. With python 3.5.1 there is no error. – Ophir Carmi Nov 17 '16 at 07:35