Newbie question

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)](http://www.renci.org)

  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

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

···

On Wed, Nov 9, 2011 at 10:07 AM, Howard <howard@…3845…> 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?

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)](http://www.renci.org)

  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

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)](http://www.renci.org)

    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)](http://www.renci.org)

  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

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!

···

On Wed, Nov 9, 2011 at 10:20 AM, Howard <howard@…3846…> wrote:

On 11/9/11 11:13 AM, Joe Kington wrote:

On Wed, Nov 9, 2011 at 10:07 AM, Howard <howard@…3845…> > > 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?