## matplotlib-users

 [Matplotlib-users] Newbie question From: - 2009-06-05 13:22:29 ```Hi, The matplotlib.collections.Collection documentation reads: "All properties in a collection must be sequences or scalars; if scalars, they will be converted to sequences. The property of the ith element of the collection is: prop[i % len(props)]". I had a look at the docstring documentation from ipython, but I didn't find out how the above is achieved (I am learning Python together with numpy, matplotlib, etc.). In my own code, how could I do something like this? If you point to a relevant location on the source code, that's alright. I also get confused sometimes because of the multiple (and sometimes interchangeable) ways of specifying arguments: sequences (list, tuples), numpy arrays, etc. I started using almost exclusively numpy arrays (probably due to my matlab background), but I am starting to mix a bit of everything now (depending on what "sources of inspiration" I use), so I wondered what a good guideline would be. Thanks, Jorge ```
 [Matplotlib-users] Newbie question From: Howard - 2011-11-09 16:07:55 Attachments: Message as HTML ```Hi all I'm a new user to matplotlib, and I'm having a little difficulty with something I feel must be basic. When I plot our data, I'm using a canvas that is 4"x4" at 128 DPI and saving the canvas as a png. Here's the basics of the code: imageWidth = 4 imageHeight = 4 DPI = 128 figure1 = plt.figure(figsize=(imageWidth,imageHeight)) plt.axis("off") plt.tricontourf(theTriangulation, modelData, theLookupTable.N, cmap=theLookupTable) canvas = FigureCanvasAgg(figure1) canvas.print_figure(prefix + ".png", dpi=DPI) The png is 512x512 as I would expect, but the contoured image doesn't fill the whole image. How do I tell the library to map the plotted are to the entire canvas and not leave a border around the rendered image? Thanks Howard -- Howard Lander Senior Research Software Developer Renaissance Computing Institute (RENCI) ; The University of North Carolina at Chapel Hill Duke University North Carolina State University 100 Europa Drive Suite 540 Chapel Hill, NC 27517 919-445-9651 ```
 Re: [Matplotlib-users] Newbie question From: Joe Kington - 2011-11-09 16:13:55 Attachments: Message as HTML ```On Wed, Nov 9, 2011 at 10:07 AM, Howard wrote: > Hi all > > I'm a new user to matplotlib, and I'm having a little difficulty with > something I feel must be basic. When I plot our data, I'm using a canvas > that is 4"x4" at 128 DPI and saving the canvas as a png. Here's the basics > of the code: > > imageWidth = 4 > imageHeight = 4 > DPI = 128 > > figure1 = plt.figure(figsize=(imageWidth,imageHeight)) > plt.axis("off") > plt.tricontourf(theTriangulation, > modelData, > theLookupTable.N, > cmap=theLookupTable) > canvas = FigureCanvasAgg(figure1) > canvas.print_figure(prefix + ".png", dpi=DPI) > > The png is 512x512 as I would expect, but the contoured image doesn't fill > the whole image. How do I tell the library to map the plotted are to the > entire canvas and not leave a border around the rendered image? > You need to make an axis that fills up the entire figure. By default, axes don't fill up the entire figure to leave room for tick labels, axis lables, titles, etc. Try something like: import matplotlib as plt dpi = 128 fig = plt.figure(figsize=(4,4)) # Specifies an axis at 0, 0 with a width and height of 1 (the full width of the figure) ax = fig.add_axes([0,0,1,1]) ax.tricontourf(...) fig.savefig('output.png', dpi=dpi) Hope that helps, -Joe ```
 Re: [Matplotlib-users] Newbie question From: Howard - 2011-11-09 16:20:13 Attachments: Message as HTML ```On 11/9/11 11:13 AM, Joe Kington wrote: > On Wed, Nov 9, 2011 at 10:07 AM, Howard > wrote: > > Hi all > > I'm a new user to matplotlib, and I'm having a little difficulty > with something I feel must be basic. When I plot our data, I'm > using a canvas that is 4"x4" at 128 DPI and saving the canvas as a > png. Here's the basics of the code: > > imageWidth = 4 > imageHeight = 4 > DPI = 128 > > figure1 = plt.figure(figsize=(imageWidth,imageHeight)) > plt.axis("off") > plt.tricontourf(theTriangulation, > modelData, > theLookupTable.N, > cmap=theLookupTable) > canvas = FigureCanvasAgg(figure1) > canvas.print_figure(prefix + ".png", dpi=DPI) > > The png is 512x512 as I would expect, but the contoured image > doesn't fill the whole image. How do I tell the library to map > the plotted are to the entire canvas and not leave a border around > the rendered image? > > > You need to make an axis that fills up the entire figure. > > By default, axes don't fill up the entire figure to leave room for > tick labels, axis lables, titles, etc. > > Try something like: > > import matplotlib as plt > > dpi = 128 > fig = plt.figure(figsize=(4,4)) > > # Specifies an axis at 0, 0 with a width and height of 1 (the full > width of the figure) > ax = fig.add_axes([0,0,1,1]) > > ax.tricontourf(...) > > fig.savefig('output.png', dpi=dpi) > > Hope that helps, > -Joe Hi Joe That did it! Thanks much. Can I also turn off the tick marks on the new axis? Howard -- Howard Lander Senior Research Software Developer Renaissance Computing Institute (RENCI) ; The University of North Carolina at Chapel Hill Duke University North Carolina State University 100 Europa Drive Suite 540 Chapel Hill, NC 27517 919-445-9651 ```
 Re: [Matplotlib-users] Newbie question From: Howard - 2011-11-09 16:32:01 Attachments: Message as HTML ```On 11/9/11 11:20 AM, Howard wrote: > On 11/9/11 11:13 AM, Joe Kington wrote: >> On Wed, Nov 9, 2011 at 10:07 AM, Howard > > wrote: >> >> Hi all >> >> I'm a new user to matplotlib, and I'm having a little difficulty >> with something I feel must be basic. When I plot our data, I'm >> using a canvas that is 4"x4" at 128 DPI and saving the canvas as >> a png. Here's the basics of the code: >> >> imageWidth = 4 >> imageHeight = 4 >> DPI = 128 >> >> figure1 = plt.figure(figsize=(imageWidth,imageHeight)) >> plt.axis("off") >> plt.tricontourf(theTriangulation, >> modelData, >> theLookupTable.N, >> cmap=theLookupTable) >> canvas = FigureCanvasAgg(figure1) >> canvas.print_figure(prefix + ".png", dpi=DPI) >> >> The png is 512x512 as I would expect, but the contoured image >> doesn't fill the whole image. How do I tell the library to map >> the plotted are to the entire canvas and not leave a border >> around the rendered image? >> >> >> You need to make an axis that fills up the entire figure. >> >> By default, axes don't fill up the entire figure to leave room for >> tick labels, axis lables, titles, etc. >> >> Try something like: >> >> import matplotlib as plt >> >> dpi = 128 >> fig = plt.figure(figsize=(4,4)) >> >> # Specifies an axis at 0, 0 with a width and height of 1 (the full >> width of the figure) >> ax = fig.add_axes([0,0,1,1]) >> >> ax.tricontourf(...) >> >> fig.savefig('output.png', dpi=dpi) >> >> Hope that helps, >> -Joe > Hi Joe > > That did it! Thanks much. Can I also turn off the tick marks on the > new axis? > > Howard For the sake of reply, this seems to work to get rid of the tick marks: ax.tick_params(axis="both", length=0, width=0) Thanks Howard > > -- > Howard Lander > Senior Research Software Developer > Renaissance Computing Institute (RENCI) ; > The University of North Carolina at Chapel Hill > Duke University > North Carolina State University > 100 Europa Drive > Suite 540 > Chapel Hill, NC 27517 > 919-445-9651 -- Howard Lander Senior Research Software Developer Renaissance Computing Institute (RENCI) ; The University of North Carolina at Chapel Hill Duke University North Carolina State University 100 Europa Drive Suite 540 Chapel Hill, NC 27517 919-445-9651 ```
 Re: [Matplotlib-users] Newbie question From: Joe Kington - 2011-11-09 16:32:53 Attachments: Message as HTML ```On Wed, Nov 9, 2011 at 10:20 AM, Howard wrote: > On 11/9/11 11:13 AM, Joe Kington wrote: > > On Wed, Nov 9, 2011 at 10:07 AM, Howard wrote: > >> Hi all >> >> I'm a new user to matplotlib, and I'm having a little difficulty with >> something I feel must be basic. When I plot our data, I'm using a canvas >> that is 4"x4" at 128 DPI and saving the canvas as a png. Here's the basics >> of the code: >> >> imageWidth = 4 >> imageHeight = 4 >> DPI = 128 >> >> figure1 = plt.figure(figsize=(imageWidth,imageHeight)) >> plt.axis("off") >> plt.tricontourf(theTriangulation, >> modelData, >> theLookupTable.N, >> cmap=theLookupTable) >> canvas = FigureCanvasAgg(figure1) >> canvas.print_figure(prefix + ".png", dpi=DPI) >> >> The png is 512x512 as I would expect, but the contoured image doesn't >> fill the whole image. How do I tell the library to map the plotted are to >> the entire canvas and not leave a border around the rendered image? >> > > You need to make an axis that fills up the entire figure. > > By default, axes don't fill up the entire figure to leave room for tick > labels, axis lables, titles, etc. > > Try something like: > > import matplotlib as plt > > dpi = 128 > fig = plt.figure(figsize=(4,4)) > > # Specifies an axis at 0, 0 with a width and height of 1 (the full width > of the figure) > ax = fig.add_axes([0,0,1,1]) > > ax.tricontourf(...) > > fig.savefig('output.png', dpi=dpi) > > Hope that helps, > -Joe > > Hi Joe > > That did it! Thanks much. Can I also turn off the tick marks on the new > axis? > > Sure! If you want an exact duplicate of your original code snippet, just add an: ax.axis('off') or one of the many equivalent ways of hiding the entire axis. If you'd rather keep the outline and white background patch, but just turn the ticks off, then you can do something like: for axis in [ax.xaxis, ax.yaxis]: ax.set_ticks([]) or similarly: ax.tick_params(color='none') Also, as you've probably already noticed, I meant to have "import matplotlib.pyplot as plt" in my first reply, rather than "import matplotlib as plt". Cheers! ```
 Re: [Matplotlib-users] Newbie question From: Eric Firing - 2009-06-12 05:56:20 ```jorgesmbox-ml@... wrote: > > Hi, The matplotlib.collections.Collection documentation reads: "All > properties in a collection must be sequences or scalars; if scalars, > they will be converted to sequences. The property of the ith element > of the collection is: prop[i % len(props)]". I had a look at the > docstring documentation from ipython, but I didn't find out how the > above is achieved (I am learning Python together with numpy, > matplotlib, etc.). In my own code, how could I do something like > this? If you point to a relevant location on the source code, that's import matplotlib.cbook as cbook def to_sequence(arg): if cbook.is_iterable(arg): return arg return [arg] Above is an example of how one can turn a scalar into a sequence (a list, in this case) if necessary. > alright. I also get confused sometimes because of the multiple (and > sometimes interchangeable) ways of specifying arguments: sequences > (list, tuples), numpy arrays, etc. I started using almost exclusively > numpy arrays (probably due to my matlab background), but I am > starting to mix a bit of everything now (depending on what "sources > of inspiration" I use), so I wondered what a good guideline would be. Different types of sequence have different advantages and disadvantages. Tuples are immutable. Lists are much more flexible, and can be extended. ndarrays are fixed-size, but facilitate efficient computation. If a function or method accepts any kind of sequence for a given argument, then probably the thing to do is give it whatever you have already, or whatever is most convenient to generate. Lists are a good default if the sequence has only a few elements and you are writing them out, rather than calculating them from some other sequence. In other words, if a function is flexible, then trust the function to do whatever conversions it needs internally; there is no particular advantage in doing the conversion yourself when you specify the argument. Eric ```
 Re: [Matplotlib-users] Newbie question From: - 2009-06-12 10:38:56 ``` ----- Mensaje original ---- > De: Eric Firing > > import matplotlib.cbook as cbook > > def to_sequence(arg): > if cbook.is_iterable(arg): > return arg > return [arg] > > Above is an example of how one can turn a scalar into a sequence (a list, in > this case) if necessary. When I enter this into ipython, I get: In [67]: to_sequence(1) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /home/jscandal/sw/python/myimports.py in () ----> 1 2 3 4 5 /home/jscandal/sw/python/myimports.py in to_sequence(arg) 1 ----> 2 3 4 5 AttributeError: 'module' object has no attribute 'is_iterable' I have matplotlib 0.98.5.2, and it seems is_iterable is not there. > Different types of sequence have different advantages and disadvantages. Tuples > are immutable. Lists are much more flexible, and can be extended. ndarrays are > fixed-size, but facilitate efficient computation. > > If a function or method accepts any kind of sequence for a given argument, then > probably the thing to do is give it whatever you have already, or whatever is > most convenient to generate. Lists are a good default if the sequence has only > a few elements and you are writing them out, rather than calculating them from > some other sequence. In other words, if a function is flexible, then trust the > function to do whatever conversions it needs internally; there is no particular > advantage in doing the conversion yourself when you specify the argument. > Thanks for the hints, I guess I get a bit frustrated or lost sometimes. Trying to get work done while being at the bottom of the learning curve might not be the best situation. I have the feeling, though, that I had to switch this way or else I would never really do it at all. I've been wanting to play with python for a long time now, but never did anything more than hello world examples. Until now ;) Jorge ```
 Re: [Matplotlib-users] Newbie question From: Eric Firing - 2009-06-12 18:54:37 ```jorgesmbox-ml@... wrote: > > > > > ----- Mensaje original ---- >> De: Eric Firing >> >> import matplotlib.cbook as cbook >> >> def to_sequence(arg): >> if cbook.is_iterable(arg): I goofed. It should be cbook.iterable(arg). Eric ```