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

Hi everyone,

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. What if I create my own set of Rectangle

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

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.

import numpy as np

import matplotlib.pyplot as plt

myFig = plt.figure()

myAx = myFig.add_axes() # I have tried myFig.add_subplot(1,1,1) but it didn’t help

x = np.arange(0,np.pi, 0.01)

myAx.plot (x, np.sin(x))

plt.draw() # nothing happens

These commands are executed within an interactive ipython session but if I start ipython

with ipython -pylab, plt.draw() draws a figure I can see. I’m running Arch linux and Openbox

as a window manager, the system is 64 bit.

2010/4/11 tomislav_maric@...2537... <tomislav.maric@...2537...>:

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?

Here http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.add_artist
maybe helps?

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. What if I create my own
set of Rectangle
(Polygon) objects and want to create a list of them and plot them?

afaik, the artist, or whatever, is retured to make its properties
adjustable later.

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.

import numpy as np
import matplotlib.pyplot as plt

myFig = plt.figure()

myAx = myFig.add_axes() # I have tried myFig.add_subplot(1,1,1) but it
didn't help

x = np.arange(0,np.pi, 0.01)

myAx.plot (x, np.sin(x))

plt.draw() # nothing happens

As your oo Figure is not registered to the plt module, since you
created it via api, this should be normal. Maybe have a look at
http://matplotlib.sourceforge.net/examples/user_interfaces/index.html
, I think you have to embed your api-created Figure in a widget
manager of your choice.

These commands are executed within an interactive ipython session but if I
start ipython

with ipython -pylab, plt.draw() draws a figure I can see. I'm running Arch
linux and Openbox

as a window manager, the system is 64 bit.

I think you can use Tk via the Tkinter Python package. On linux I
heard it's looking a bit weird, but as a starting points it's easy
enough. But maybe try also the other widget managers, like Gtk.
There are certainly some people around which have more knowledge on
Gtk and so on than me having with Tkinter.

hth,
Friedrich

Weird how?
Will that be fixed with the new release (ttk, in Python 2.7)?

Thanks,
Alan Isaac

···

On 4/11/2010 9:27 AM, Friedrich Romstedt wrote:

I think you can use Tk via the Tkinter Python package. On linux I
heard it's looking a bit weird, but as a starting points it's easy
enough.

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

  SciPy 2009 - Advanced tutorial 3: Advanced topics in matplotlib : Free Download, Borrow, and Streaming : Internet Archive

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
  Search — Matplotlib 3.8.2 documentation

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

···

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

2010/4/11 Alan G Isaac <alan.isaac@...287...>:

I think you can use Tk via the Tkinter Python package. On linux I
heard it's looking a bit weird, but as a starting points it's easy
enough.

Weird how?
Will that be fixed with the new release (ttk, in Python 2.7)?

Hmm, it was meant as a warning, nothing shure. I just read somewhere
that the Tk, although originating from linux, would look the worst on
linux. I can neither verify nor falsify, because I have no linux.

Sorry for the noise. I even cannot exclude that it's wrong.

This amazing page: http://docs.python.org/dev/library/ttk.html (I
especially like the ComboBox, something I always missed in Tkinter :slight_smile:
states that look and feed would change with ttk, so maybe you're
lucky.

Friedrich

···

On 4/11/2010 9:27 AM, Friedrich Romstedt wrote: