From: <tom...@gm...> - 2010-04-11 12:18:37
|
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. |
From: Friedrich R. <fri...@gm...> - 2010-04-11 13:27:10
|
2010/4/11 tom...@gm... <tom...@gm...>: > 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 |
From: Alan G I. <ala...@gm...> - 2010-04-11 13:40:02
|
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. Weird how? Will that be fixed with the new release (ttk, in Python 2.7)? Thanks, Alan Isaac |
From: Friedrich R. <fri...@gm...> - 2010-04-12 20:09:10
|
2010/4/11 Alan G Isaac <ala...@gm...>: > 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. > > 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 :-) states that look and feed would change with ttk, so maybe you're lucky. Friedrich |
From: John H. <jd...@gm...> - 2010-04-11 14:19:39
|
On Sun, Apr 11, 2010 at 7:15 AM, tom...@gm... <tom...@gm...> 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 |