imshow without resampling

Hello,

I am using matplotlib to create postscript and SVG files. I am currently using imshow to show the contents of an array, but this means that when saving vector graphics files, matplotlib resamples the image/array onto a finer grid. What I would like, is for code such as this:

import matplotlib
matplotlib.use('PS')
from matplotlib.pyplot import *

import numpy as np

image = np.random.random((10,10))

fig = figure(figsize=(4,4))
ax = fig.add_subplot(111)
ax.imshow(image)
fig.savefig('example1.ps')

fig = figure(figsize=(8,8))
ax = fig.add_subplot(111)
ax.imshow(image)
fig.savefig('example2.ps')

to produce files that are the roughly the same size, rather than different by a factor of 4. In addition, both files should be very small since they should only contain a 10x10 bitmap in addition to the axes+labels. Postscript and SVG (as languages) both allow a bitmap of an arbitrary resolution to be scaled, translated, and rotated without resampling.

I have come across the figimage method which is meant to place an array in a plot without resampling, but I cannot figure out how to use it like imshow, i.e. to show the image inside the axes as before. I've also tried the pcolor functions, but it seems like they define each pixel as an individual polygon, which is inefficient.

I was wondering if anyone had a solution to this, or if there are plans to make matplotlib behave like this in future?

Thanks,

Thomas

I believe pcolor will be closer to what you are looking for.7

JDH

···

On Tue, Mar 31, 2009 at 2:56 PM, Thomas Robitaille <thomas.robitaille@...287...> wrote:

Hello,

I am using matplotlib to create postscript and SVG files. I am
currently using imshow to show the contents of an array, but this
means that when saving vector graphics files, matplotlib resamples the
image/array onto a finer grid. What I would like, is for code such as
this:

The problem with pcolor is that it draws all the pixels as polygons, which is very inefficient in terms of file size. For example,

import matplotlib
matplotlib.use('PS')
from matplotlib.pyplot import *

import numpy as np

image = np.random.random((100,100))

fig = figure(figsize=(4,4))
ax = fig.add_subplot(111)
ax.pcolor(image)
fig.savefig('plot.ps')

produces a 2Mb+ file.

Thanks,

Thomas

···

On Mar 31, 2009, at 4:32 PM, John Hunter wrote:

On Tue, Mar 31, 2009 at 2:56 PM, Thomas Robitaille > <thomas.robitaille@...287...> wrote:

Hello,

I am using matplotlib to create postscript and SVG files. I am
currently using imshow to show the contents of an array, but this
means that when saving vector graphics files, matplotlib resamples the
image/array onto a finer grid. What I would like, is for code such as
this:

I believe pcolor will be closer to what you are looking for.7

JDH

I've looked into this some more, and while pcolor draws pixels inefficiently as polygons, pcolorfast does appear to be writing the data as a bitmap into the eps file, so this is closer to what I need.

However, while this works great with the SVG driver, it seems that pcolorfast still produces quite large files with the PS driver. For example,

import matplotlib
matplotlib.use('Agg')
from matplotlib.pyplot import *
import numpy as np

image = np.random.random((100,100))

fig = figure(figsize=(4,4))
ax = fig.add_subplot(111)
ax.pcolorfast(image)
fig.savefig('plot.eps')

fig = figure(figsize=(4,4))
ax = fig.add_subplot(111)
ax.pcolorfast(image)
fig.savefig('plot.svg')

produces a 600k ps file and a 60k svg file. In fact, the PS file size remains the same if I change the size of the array.

So pcolorfast does appear to be the solution, but the output is not properly handled for PS files. Is this something that could be fixed in future?

Thanks,

Thomas

···

On Mar 31, 2009, at 4:32 PM, John Hunter wrote:

On Tue, Mar 31, 2009 at 2:56 PM, Thomas Robitaille > <thomas.robitaille@...287...> wrote:

Hello,

I am using matplotlib to create postscript and SVG files. I am
currently using imshow to show the contents of an array, but this
means that when saving vector graphics files, matplotlib resamples the
image/array onto a finer grid. What I would like, is for code such as
this:

I believe pcolor will be closer to what you are looking for.7

JDH