how to plot a Polygon / plt.draw() problem

Thank you very much, you helped me a great deal. The video tutorials are great.

I'm going through the stuff you sent and things seem to become clearer....

Tomislav

···

----- Original Message -----
From: John Hunter
Sent: 04/11/10 04:19 PM
To: tomislav_maric@...2537...
Subject: Re: [Matplotlib-users] how to plot a Polygon / plt.draw() problem

On Sun, Apr 11, 2010 at 7:15 AM, tomislav_maric@...2537... <tomislav.maric@...2537...> wrote:

can someone help me to plot a polygon in matplotlib?

I have been reading about the axes.patches.Polygon class and I have defined
the

Polygon object that has a preset lw and points. How do I plot it?

I'm confused because the Axes documentation states that this class holds
most of

the figure objects like Rectangle, Line2D, and then the website states that
the Line2D

is a return object from the plt.plot() invocation.

Yes, Axes.plot is a helper function which creates a Line2D object,
adds it to the axes, sets the transformation, etc... This process is
covered in some detail in the matplotlib Artist tutorial

http://matplotlib.sourceforge.net/users/artists.html

and in the advanced matplotlib tutorial at scipy -- video available here

http://www.archive.org/details/scipy09_advancedTutorialDay1_3

What if I create my own
set of Rectangle

(Polygon) objects and want to create a list of them and plot them?

If you create your own polygons/rectangles/patches, create them, and
then add them with Axes.add_patch

http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.add_patch

If you want to create a bunch of them, consider a PolygonCollection
(or a RegularPolygonCollection depending on your use case)

http://matplotlib.sourceforge.net/api/collections_api.html
http://matplotlib.sourceforge.net/search.html?q=codex+PolyCollection

Also, I'm using this sequence of commands to work in OO mode interactively

(just to learn) but when I execute plt.draw() no figure appears.

We make a distinction between raising a figure (plt.show) and
rendering to an existing figure (plt.draw). In interactive mode
(which is what ipython -pylab turns on) figures are automatically
raised/shown. You can control these settings from a regular python
shell using ion and ioff. See

http://matplotlib.sourceforge.net/users/shell.html

Here is a complete example::

import matplotlib.patches as patches
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

verts = [0,0], [0,1], [1,1], [1,0]

poly = patches.Polygon(verts)

ax.add_patch(poly)

ax.set_xlim(-2,2)
ax.set_ylim(-2,2)

plt.show()

Hope this helps,
JDH