File: math_text.py

package info (click to toggle)
scitools 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 4,148 kB
  • ctags: 3,332
  • sloc: python: 34,714; sh: 112; makefile: 8
file content (76 lines) | stat: -rwxr-xr-x 2,921 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python

"""
Demonstration on how to deal with greek letters and mathematics
in axis labels, titles, legends, and text.
"""
from scitools.std import *

def do_plot(latex='no'):
    tau_1 = linspace(0, 3, 151)
    tau_2 = linspace(0, 3, 11)
    alpha = 1
    theta_1 = tau_1*exp(-alpha*tau_1**2)
    theta_2 = sqrt(tau_2)*exp(-alpha*tau_2**2)
    if latex == 'with $':
        # standard latex text in legends, labels, and title
        plot(tau_1, theta_1, 'r-',
             tau_2, theta_2, 'bo',
             legend=(r'$\theta_1 = \tau e^{-\alpha\tau^2}$',
                     r'$\theta_2 = \sqrt{\tau} e^{-\alpha\tau^2}$'),
             title=r'Plot of $\theta_1$ and $\theta_2$, $\alpha = \sum_{i=1}^n \phi_i$' + ' (%s) ' % latex.replace('$', 'dollar'),
             xlabel=r'$\tau$',
             ylabel=r'$\theta$',
             savefig='tmp_' + backend + '_' + latex.replace(' $', '_dollar') + '.eps')
    elif latex == 'without $':
        # latex math without dollars - some backends will automatically
        # treat this the right way
        plot(tau_1, theta_1, 'r-',
             tau_2, theta_2, 'bo',
             legend=(r'\theta_1 = \tau e^{-\alpha\tau^2}',
                     r'\theta_2 = \sqrt{\tau} e^{-\alpha\tau^2}'),
             title=r'Plot of \theta_1 and \theta_2, \alpha = \sum_{i=1}^n \phi_i' + ' (%s) ' % latex.replace('$', 'dollar'),
             xlabel=r'\tau',
             ylabel=r'\theta',
             savefig='tmp_' + backend + '_' + latex.replace(' $', '_dollar') + '.eps')
    elif latex == 'no':
        # No latex math, just plain words
        plot(tau_1, theta_1, 'r-',
             tau_2, theta_2, 'bo',
             legend=(r'theta_1 = tau exp(-alpha*tau^2)',
                     r'theta_2 = sqrt(tau)*exp(-\alpha*tau^2)'),
             title='Plot of theta_1 and theta_2, alpha = sum_i phi_i' + ' (%s) ' % latex.replace('$', 'dollar'),
             xlabel=r'tau',
             ylabel=r'theta',
             savefig='tmp_' + backend + '_' + latex + '.eps')

print backend
for latex in 'no', 'with $', 'without $':
    figure()
    print 'latex: %s' % latex
    do_plot(latex)
raw_input('Press Return: ')

"""
Summary:

=== gnuplot ===

Plain text (no latex, no dollars) works best. Output in .eps file
will recognize sub and super scripts, and also many greek letters if
the letters are written in latex syntax with an opening backslash.
For .eps hardcopies a little tweaking with inserting some
backslashes if often desirable.

Lables like \tau and \theta get typeset as tab "au" and tab "heta"
due to file communication between python and gnuplot (just skip
the backslashes to avoid this, raw strings do not help - then the
backslash survives).

=== matplotlib ===

Labels and legends work well with and without dollars. The title
should be plain text with embedded dollars and latex as needed.
Real latex text is what is recommended for matplotlib.

"""