Use a marker symbol in a text string

Hi:

I want to use the symbol corresponding to a marker in a text
annotation. Something like

textstr = 'This is the square marker: ?'
ax.text(0.05, 0.95, textstr)

Is there something I can place where the question mark is above to get
the actual square (or any other of the symbols you can use as a
marker)?

This is similar to what the legend command does, but I need more flexibility.

Alejandro.

As I have learnt from Michael Droettboom, you can simply use unicode
characters with a supported font set:
In my setup I prefer DejaVu-Sans. First install these fonts into your
system, then edit your matplotlibrc

font.family : sans-serif
font.sans-serif : DejaVu Sans

or alternatively using your favorite unicode font.

then try this example (suggest using a unicode supported text editor
(e.g., vim)):

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt

plt.plot(range(100))
plt.text(0.05, 0.95, u"This is the square marker: □", fontsize=20)
plt.show()

Unicode characters are great for putting units, but it is not easy to
construct complex equations. Does anyone know if there is
sub/super-script range alphabet (not only numbers) supported unicode
font set?

···

On Tue, Nov 23, 2010 at 10:22 AM, Alejandro Weinstein <alejandro.weinstein@...287...> wrote:

Hi:

I want to use the symbol corresponding to a marker in a text
annotation. Something like

textstr = 'This is the square marker: ?'
ax.text(0.05, 0.95, textstr)

Is there something I can place where the question mark is above to get
the actual square (or any other of the symbols you can use as a
marker)?

This is similar to what the legend command does, but I need more flexibility.

Alejandro.

--
Gökhan

Why not just use latex?

plt.text(0.05, 0.95, r"This is the square marker: $\Box$ Subscript: $_\Box$ superscript: $^\Box$", fontsize=20)

It produces the following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
  File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_tkagg.py",
line 212, in resize
    self.show()
  File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_tkagg.py",
line 215, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_agg.py",
line 314, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/pymodules/python2.6/matplotlib/artist.py", line 46,
in draw_wrapper
    draw(artist, renderer, *kl)
  File "/usr/lib/pymodules/python2.6/matplotlib/figure.py", line 773, in draw
    for a in self.axes: a.draw(renderer)
  File "/usr/lib/pymodules/python2.6/matplotlib/artist.py", line 46,
in draw_wrapper
    draw(artist, renderer, *kl)
  File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 1735, in draw
    a.draw(renderer)
  File "/usr/lib/pymodules/python2.6/matplotlib/text.py", line 515, in draw
    bbox, info = self._get_layout(renderer)
  File "/usr/lib/pymodules/python2.6/matplotlib/text.py", line 279, in
_get_layout
    clean_line, self._fontproperties, ismath=ismath)
  File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_agg.py",
line 156, in get_text_width_height_descent
    self.mathtext_parser.parse(s, self.dpi, prop)
  File "/usr/lib/pymodules/python2.6/matplotlib/mathtext.py", line
2810, in parse
    box = self._parser.parse(s, font_output, fontsize, dpi)
  File "/usr/lib/pymodules/python2.6/matplotlib/mathtext.py", line
2259, in parse
    self._expression.parseString(s)
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
1048, in parseString
    loc, tokens = self._parse( instring, 0 )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
981, in _parseCache
    value = self._parseNoCache( instring, loc, doActions, callPreParse )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
924, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
2559, in parseImpl
    return self.expr._parse( instring, loc, doActions, callPreParse=False )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
981, in _parseCache
    value = self._parseNoCache( instring, loc, doActions, callPreParse )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
924, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
2307, in parseImpl
    loc, exprtokens = e._parse( instring, loc, doActions )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
981, in _parseCache
    value = self._parseNoCache( instring, loc, doActions, callPreParse )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
924, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
2672, in parseImpl
    loc, tokens = self.expr._parse( instring, loc, doActions,
callPreParse=False )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
981, in _parseCache
    value = self._parseNoCache( instring, loc, doActions, callPreParse )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
924, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
2307, in parseImpl
    loc, exprtokens = e._parse( instring, loc, doActions )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
981, in _parseCache
    value = self._parseNoCache( instring, loc, doActions, callPreParse )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
924, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
2416, in parseImpl
    ret = e._parse( instring, loc, doActions )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
981, in _parseCache
    value = self._parseNoCache( instring, loc, doActions, callPreParse )
  File "/usr/lib/pymodules/python2.6/matplotlib/pyparsing.py", line
950, in _parseNoCache
    tokens = fn( instring, tokensStart, retTokens )
  File "/usr/lib/pymodules/python2.6/matplotlib/mathtext.py", line
1997, in raise_error
    raise ParseFatalException(msg + "\n" + s)
ParseFatalException: Expected end of math '&#39; This is the square marker: \Box$ Subscript (at char 0), (line:1, col:1)

···

On Tue, Nov 23, 2010 at 8:15 PM, Nicolas Bigaouette <nbigaouette@...287...> wrote:

Why not just use latex?
plt.text(0.05, 0.95, r"This is the square marker: \\Box Subscript: \_\\Box
superscript: ^\\Box", fontsize=20)

Thanks for the answer. But I think there must be an easier way without
the need to install anything. After all, the legend command is able to
create text using the markers.

Alejandro.

···

On Tue, Nov 23, 2010 at 9:37 AM, Gökhan Sever <gokhansever@...287...> wrote:

As I have learnt from Michael Droettboom, you can simply use unicode
characters with a supported font set:
In my setup I prefer DejaVu-Sans. First install these fonts into your
system, then edit your matplotlibrc

You have three options then:

1-) Figure out how legend handles symbol drawing.
2-) Use latex / mathtext to represent symbols.
3-) Install a font and more intuitively insert a sqaure box into your string.

For me, as long as the expression that is drawn on screen is not
complicated, I am very happy using the unicode text approach.

···

On Wed, Nov 24, 2010 at 8:38 AM, Alejandro Weinstein <alejandro.weinstein@...287...> wrote:

On Tue, Nov 23, 2010 at 9:37 AM, Gökhan Sever <gokhansever@...287...> wrote:

As I have learnt from Michael Droettboom, you can simply use unicode
characters with a supported font set:
In my setup I prefer DejaVu-Sans. First install these fonts into your
system, then edit your matplotlibrc

Thanks for the answer. But I think there must be an easier way without
the need to install anything. After all, the legend command is able to
create text using the markers.

Alejandro.

--
Gökhan

Here is the example that I believe that does what you want.
The way it does is very similar to how legend object is created.
Unfortunately, the offsetbox module, which enables this example, is
not well documented. I hope the example is explanatory enough.

IHTH,

-JJ

import matplotlib.pyplot as plt
from matplotlib.offsetbox import TextArea, DrawingArea, HPacker, AnnotationBbox
import matplotlib.lines as mlines

ax = plt.subplot(111)

fontsize=15
box1 = TextArea("This is the square marker :", textprops=dict(color="k",
                                                              size=fontsize))

# create a drwaing area for a marker
# It will have width*height = fontsize*0.8fontsize
# and xdescent, ydescent = 0*0.2fontsize
box2 = DrawingArea(fontsize, 0.8*fontsize,
                   0, 0.2*fontsize)

# maker
l1 = mlines.Line2D([0.5*fontsize], [0.25*fontsize], marker="s",
                   markersize=(0.5*fontsize))

# add the marker to the drawing area
box2.add_artist(l1)

# now combine the textarea and the drawing area (marker)
box = HPacker(children=[box1, box2],
              align="baseline",
              pad=0, sep=fontsize*0.2)

# Use AnnotationBbox object using *box*. Note that the AnnotationBbox
is very similar to annotate. It uses offsetbox object instead of the
text object

a_box = AnnotationBbox(box, xy=(0., 1.),
                       xycoords='data',
                       xybox=(fontsize, -fontsize),
                       boxcoords="offset points",
                       box_alignment=(0., 1),
                       frameon=True,
                       )

ax.add_artist(a_box)

plt.show()

···

On Wed, Nov 24, 2010 at 1:22 AM, Alejandro Weinstein <alejandro.weinstein@...287...> wrote:

Hi:

I want to use the symbol corresponding to a marker in a text
annotation. Something like

textstr = 'This is the square marker: ?'
ax.text(0.05, 0.95, textstr)

Is there something I can place where the question mark is above to get
the actual square (or any other of the symbols you can use as a
marker)?

This is similar to what the legend command does, but I need more flexibility.

Alejandro.

------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options