TeX $\times$ symbol not working in matplotlib?

G

gwhite

Hi,

I am trying to understand how to get the TeX "\times" symbol to work. It is in the title() string in the code I pasted in. The "\circ" symbol seems fine, by comparison. "\times" ends up as "imes" in the figure title.

I am probably doing something dumb (hey, maybe a lot of dumb things!), but if you can spot and describe my mistake, I would be quite happy about that.

Thank you.

# (Using Python 2.7.5 via pythonxy. w7-64)

----------------------------------
import numpy as np
import matplotlib.pyplot as plt

Qs_rr = np.array([0.])
Qs_ri = np.array([0.])
Es_rr = np.array([-6.352844845095E-02,\
-6.352844845095E-02,\
-9.917112781473E-01,\
-1.008084892264E+00,\
-5.534164139252E-02,\
-5.534164139252E-02])
Es_ri = np.array([ 9.329580097745E-01,\
-9.329580097745E-01,\
0.000000000000E+00,\
0.000000000000E+00,\
1.070772729531E+00,\
-1.070772729531E+00])
plt.hold(False)
figs_open = plt.get_fignums()
axes_obj=plt.figure(figs_open[0]).gca()
lh1 = plt.plot(Qs_rr, Qs_ri, 'ro',\
Es_rr, Es_ri, 'rx')
lh1[0].set_markerfacecolor('w')
lh1[0].set_markeredgecolor('r')
lh1[0].set_markersize(9.0)
lh1[0].set_markeredgewidth(0.75)
lh1[1].set_markersize(9.0)
lh1[1].set_markeredgewidth(0.75)
plt.axis([-1.2, 0.2, -1.2, 1.2])
plt.grid(True)
plt.title('$\mathrm{poles}$ $(\times)$ \
$\mathrm{\&}$ $\mathrm{zeros}$ \
$(\circ)$ $\mathrm{of}$ $T(s)T(-s)$',\
fontsize=16)
plt.xlabel(r'$\sigma$', fontsize=16)
plt.ylabel(r'$\mathrm{j}\omega$', fontsize=16)
plt.show()
 
C

Chris “Kwpolska†Warrick

I am trying to understand how to get the TeX "\times" symbol to work. Itis in the title() string in the code I pasted in. The "\circ" symbol seems fine, by comparison. "\times" ends up as "imes" in the figure title.

I am probably doing something dumb (hey, maybe a lot of dumb things!), but if you can spot and describe my mistake, I would be quite happy about that.
plt.title('$\mathrm{poles}$ $(\times)$ \
$\mathrm{\&}$ $\mathrm{zeros}$ \
$(\circ)$ $\mathrm{of}$ $T(s)T(-s)$',\
fontsize=16)

You’re using a regular string. In which backspaces can be used in
escapes. \t is one of those escapes, it is the tab character. In
order to fix, add the letter "r" before the opening quote. Like this:
plt.title(r'$\mathrm{poles}$ $(\times)$ \
$\mathrm{\&}$ $\mathrm{zeros}$ \
$(\circ)$ $\mathrm{of}$ $T(s)T(-s)$',\
fontsize=16)

Moreover, in the next two things, you already did it right in the first place:
 
G

gwhite

You're using a regular string. In which backspaces can be used in
escapes. \t is one of those escapes, it is the tab character. In
order to fix, add the letter "r" before the opening quote. Like this:


Moreover, in the next two things, you already did it right in the first place:


Thanks Chris! That worked.

I faked myself out since the $(\circ)$ worked *without* the little r prefix. I was blind to it. I guess the difference must be there is no \c thingy to override \circ, so it just does the circle.

Thanks for the note on how the r prefix works. I knew I would screw myself sooner or later on that.

Getting regular text mixed with math text, but with the same font (in regular or italic) is a bit clumsy, I suppose. (I mean getting the spaces in.) At least I can do it.

I did this too, and it also seems to work:

plt.title(' '.join([r'$\mathrm{poles}$', r'$(\times)$',\
r'$\mathrm{\&}$', r'$\mathrm{zeros}$',
r'$(\circ)$', r'$\mathrm{of}$',\
r'$T(s)T(-s)$']), fontsize=16)
 
P

Peter Otten

gwhite said:
plt.title(' '.join([r'$\mathrm{poles}$', r'$(\times)$',\
r'$\mathrm{\&}$', r'$\mathrm{zeros}$',
r'$(\circ)$', r'$\mathrm{of}$',\
r'$T(s)T(-s)$']), fontsize=16)

Note that adjacent string literals on the same line or inside parentheses
are automatically concatenated by the compiler. So you may write the above
as

plt.title(
r'$\mathrm{poles}$ $(\times)$ '
r'$\mathrm{\&}$ $\mathrm{zeros}$ '
r'$(\circ)$ $\mathrm{of}$ $T(s)T(-s)$',
fontsize=16)

Even if you leave everything else as is you don't need any backslashes at
the end of the line.
 
D

Dave Angel

gwhite said:
Hi,

I am trying to understand how to get the TeX "\times" symbol to work. It is in the title() string in the code I pasted in. The "\circ" symbol seems fine, by comparison. "\times" ends up as "imes" in the figure title.

I am probably doing something dumb (hey, maybe a lot of dumb things!), but if you can spot and describe my mistake, I would be quite happy about that.

You want a raw string, as you did correctly in two other places
in the code. A raw string tells Python not to use the backslash
as an escape.

(Chris said backspace, but he meant backslash)

You specify raw string by the letter r just before the quote.
$\mathrm{\&}$ $\mathrm{zeros}$ \
$(\circ)$ $\mathrm{of}$ $T(s)T(-s)$',\
fontsize=16)

Change to:

plt.title(r'$\mathrm{poles}$ $(\times)$ \
$\mathrm{\&}$ $\mathrm{zeros}$ \
$(\circ)$ $\mathrm{of}$ $T(s)T(-s)$',\
fontsize=16)
 
G

gwhite

gwhite said:
plt.title(' '.join([r'$\mathrm{poles}$', r'$(\times)$',\
r'$\mathrm{\&}$', r'$\mathrm{zeros}$',
r'$(\circ)$', r'$\mathrm{of}$',\
r'$T(s)T(-s)$']), fontsize=16)

Note that adjacent string literals on the same line or inside parentheses
are automatically concatenated by the compiler. So you may write the above
as

plt.title(
r'$\mathrm{poles}$ $(\times)$ '
r'$\mathrm{\&}$ $\mathrm{zeros}$ '
r'$(\circ)$ $\mathrm{of}$ $T(s)T(-s)$',
fontsize=16)

Even if you leave everything else as is you don't need any backslashes at
the end of the line.

Well even if it had been right, I omitted one (backslash). I'm such a newb/hack. lol. No animals were harmed.

Yeah, I have noticed that they don't seem to be needed, but I think I remember reading "someplace-somewhere" that a backslash means a line continuation, and perhaps I saw some author put them in. So I did it out of trying to be "strict."

I'm not sure when a backslash continuation might be needed, or if that requirement has been designed out of Python.

Anyway, thanks to all for the notes!
 
G

gwhite

[snip]
I'm not sure when a backslash continuation might be needed, or if that requirement has been designed out of Python.

['they' meaning trailing backslashes]

No, 'they' are still definitely in Python, but can usually be avoided.

As already mentioned, strings are automatically concatenated if they are separated by only
whitespace (spaces/tabs/newlines). But there is a difference between this concatenation and
using a trailing backslash. For example:

print('this, that, '
'the other')

gives -> 'this, that, the other'

print('this, that, \
the other')

gives -> 'this, that, the other'

The leading whitespace in the second example is significant, but is ignored in the first.

The other places you can avoid the trailing backslash is within brackets, ie. (), [] or {}.
Here you can split at any 'natural' position, that is following a comma, dot or an operator.

['spam',
'eggs',
'bacon']

gives -> ['spam', 'eggs', 'bacon']

---------
[2 +
3,
'spam']

gives -> [5, 'spam']

---------
print('this' and
'that' or
'other')

gives -> 'that'

---------
print('{}'.
format('spam'))

gives -> 'spam'

These examples are somewhat contrived, but they do show what I'm talking about.

Of course, you can still use the trailing backslash method, but when you can avoid it it usually
looks cleaner. Besides simply using either method to split long lines, it is often used to line
things up, either for the appearance or for documentation. Here is a dictionary example of what
I mean (and the backslash method will NOT work here):

d = {1 : 'one', # Describe this key/value pair
2 : 'two', # Describe this one
3 : 'three' # Etc.
}

Play around in the interactive mode to check out how this splitting works.

Thank you, Larry. Your concise examples are nicely illustrative of the essentials. I appreciate the explanation.

Thanks again to everyone.

If I had the time, I would become a Python addict.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top