Figure.draw_artist() bug with Text

I am getting a fault when I try to use Figure.draw_artist() with a matplotlib.text.Text object. Since matplotlib.text.Text inherits from matplotlib.artist.Artist, which is what draw_artist() takes, this should probably work.

Tested with latest SVN code on Linux.

Here is the traceback:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    fig.draw_artist(textartist)
  File "/usr/local/lib/python2.6/site-packages/matplotlib/figure.py", line 816, in draw_artist
    a.draw(self._cachedRenderer)
  File "/usr/local/lib/python2.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *kl)
  File "/usr/local/lib/python2.6/site-packages/matplotlib/text.py", line 549, in draw
    bbox, info = self._get_layout(renderer)
  File "/usr/local/lib/python2.6/site-packages/matplotlib/text.py", line 267, in _get_layout
    key = self.get_prop_tup()
  File "/usr/local/lib/python2.6/site-packages/matplotlib/text.py", line 716, in get_prop_tup
    self.figure.dpi, id(self._renderer),
AttributeError: 'NoneType' object has no attribute 'dpi'

And here is some simple code to trigger the bug:

#!/usr/bin/env python
# display bug in figure.draw_artist(matplotlib.text)
import matplotlib.pyplot as plt
from matplotlib.text import Text

fig = plt.figure()
plt.draw()

textartist = Text(0.5, 0.5, "Foo")
fig.draw_artist(textartist)

plt.show()
#end code

Note that I still get the bug even when i specify figsize and dpi on the figure like so:
fig = plt.figure(figsize=(2,2), dpi=300)

-Ben

This is not a bug.
The exception is raised simply because "textartist.figure" is None
(and it is None because you never set it).
"textartist" you created is not properly set up (no figure, no axes,
no transform). You may do

textartist = Text(0.5, 0.5, "Foo")
textartist.set_figure(fig)
fig.draw_artist(textartist)
fig.canvas.blit(fig.bbox)

But, this is not the recommended way of doing things.
draw_artist is mainly for doing animation.

http://matplotlib.sourceforge.net/examples/animation/index.html

Regards,

-JJ

···

On Mon, Feb 8, 2010 at 4:24 PM, Ben Axelrod <BAxelrod@...2066...> wrote:

I am getting a fault when I try to use Figure.draw_artist() with a matplotlib.text.Text object. Since matplotlib.text.Text inherits from matplotlib.artist.Artist, which is what draw_artist() takes, this should probably work.

Tested with latest SVN code on Linux.

Here is the traceback:

Traceback (most recent call last):
File "test.py", line 10, in <module>
fig.draw_artist(textartist)
File "/usr/local/lib/python2.6/site-packages/matplotlib/figure.py", line 816, in draw_artist
a.draw(self._cachedRenderer)
File "/usr/local/lib/python2.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *kl)
File "/usr/local/lib/python2.6/site-packages/matplotlib/text.py", line 549, in draw
bbox, info = self._get_layout(renderer)
File "/usr/local/lib/python2.6/site-packages/matplotlib/text.py", line 267, in _get_layout
key = self.get_prop_tup()
File "/usr/local/lib/python2.6/site-packages/matplotlib/text.py", line 716, in get_prop_tup
self.figure.dpi, id(self._renderer),
AttributeError: 'NoneType' object has no attribute 'dpi'

And here is some simple code to trigger the bug:

#!/usr/bin/env python
# display bug in figure.draw_artist(matplotlib.text)
import matplotlib.pyplot as plt
from matplotlib.text import Text

fig = plt.figure()
plt.draw()

textartist = Text(0.5, 0.5, "Foo")
fig.draw_artist(textartist)

plt.show()
#end code

Note that I still get the bug even when i specify figsize and dpi on the figure like so:
fig = plt.figure(figsize=(2,2), dpi=300)

-Ben
------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

I see. But why would I need to set the figure manually when I am drawing with a figure? Is it ever the case where you set one figure, but draw with another? For example:

textartist.set_figure(fig1)
fig2.draw_artist(textartist)

Also, other atists don't fail in this manner if I don't use artist.set_figure().

-Ben

···

-----Original Message-----
From: Jae-Joon Lee [mailto:lee.j.joon@…287…]
Sent: Monday, February 08, 2010 6:36 PM
To: Ben Axelrod
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Figure.draw_artist() bug with Text

This is not a bug.
The exception is raised simply because "textartist.figure" is None (and it is None because you never set it).
"textartist" you created is not properly set up (no figure, no axes, no transform). You may do

textartist = Text(0.5, 0.5, "Foo")
textartist.set_figure(fig)
fig.draw_artist(textartist)
fig.canvas.blit(fig.bbox)

But, this is not the recommended way of doing things.
draw_artist is mainly for doing animation.

http://matplotlib.sourceforge.net/examples/animation/index.html

Regards,

-JJ

On Mon, Feb 8, 2010 at 4:24 PM, Ben Axelrod <BAxelrod@...2066...> wrote:

I am getting a fault when I try to use Figure.draw_artist() with a matplotlib.text.Text object. Since matplotlib.text.Text inherits from matplotlib.artist.Artist, which is what draw_artist() takes, this should probably work.

Tested with latest SVN code on Linux.

Here is the traceback:

Traceback (most recent call last):
File "test.py", line 10, in <module>
fig.draw_artist(textartist)
File "/usr/local/lib/python2.6/site-packages/matplotlib/figure.py",
line 816, in draw_artist
a.draw(self._cachedRenderer)
File "/usr/local/lib/python2.6/site-packages/matplotlib/artist.py",
line 55, in draw_wrapper
draw(artist, renderer, *kl)
File "/usr/local/lib/python2.6/site-packages/matplotlib/text.py",
line 549, in draw
bbox, info = self._get_layout(renderer)
File "/usr/local/lib/python2.6/site-packages/matplotlib/text.py",
line 267, in _get_layout
key = self.get_prop_tup()
File "/usr/local/lib/python2.6/site-packages/matplotlib/text.py",
line 716, in get_prop_tup
self.figure.dpi, id(self._renderer),
AttributeError: 'NoneType' object has no attribute 'dpi'

And here is some simple code to trigger the bug:

#!/usr/bin/env python
# display bug in figure.draw_artist(matplotlib.text)
import matplotlib.pyplot as plt
from matplotlib.text import Text

fig = plt.figure()
plt.draw()

textartist = Text(0.5, 0.5, "Foo")
fig.draw_artist(textartist)

plt.show()
#end code

Note that I still get the bug even when i specify figsize and dpi on the figure like so:
fig = plt.figure(figsize=(2,2), dpi=300)

-Ben
----------------------------------------------------------------------
-------- The Planet: dedicated and managed hosting, cloud storage,
colocation Stay online with enterprise data centers and the best
network in the business Choose flexible plans and management services
without long-term contracts Personal 24x7 support from experience
hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Figure.draw_artist is just a convenience function.

    def draw_artist(self, a):
        """
        draw :class:`matplotlib.artist.Artist` instance *a* only --
        this is available only after the figure is drawn
        """
        assert self._cachedRenderer is not None
        a.draw(self._cachedRenderer)

And, all the drawing is done by the artist itself.

Also, other atists don't fail in this manner if I don't use artist.set_figure().

As far as I can see, it only means that you're lucky with other artists.
The reason Text artists needs a reference to the figure is to access
the dpi value, as the exception indicates.
All artists are meant to be added to an axes (or at least "axes",
"figure", etc. attributes set appropriately) to work correctly.

Regards,

-JJ