2

I have a numpy array A, having a shape of (60,60,3), and I am using:

plt.imshow(  A,
             cmap          = plt.cm.gist_yarg,
             interpolation = 'nearest',
             aspect        = 'equal',
             shape         = A.shape
             )
plt.savefig( 'myfig.png' )

When I examine the myfig.png file, I see that it is 1200x800 pixels (in color).

What's going on here? I was expecting a 60x60 RGB image.

user3666197
  • 1
  • 6
  • 50
  • 92
Fequish
  • 705
  • 6
  • 17

3 Answers3

2

matplotlib doesn't work directly with pixels but rather a figure size (in inches) and a resolution (dots per inch, dpi)

So, you need to explicitly give a figure size and dpi. For example, you could set your figure size to 1x1 inches, then set the dpi to 60, to get a 60x60 pixel image.

You also have to remove the whitespace around the plot area, which you can do with subplots_adjust

import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(1,1))

A = np.random.rand(60,60,3)

plt.imshow(A,
           cmap=plt.cm.gist_yarg,
           interpolation='nearest',
           aspect='equal',
           shape=A.shape)

plt.subplots_adjust(left=0,right=1,bottom=0,top=1)

plt.savefig('myfig.png',dpi=60)

That creates this figure:

enter image description here

Which has a size of 60x60 pixels:

$ identify myfig.png 
myfig.png PNG 60x60 60x60+0+0 8-bit sRGB 8.51KB 0.000u 0:00.000

You might also refer to this answer, which has lots of good information about figure sizes and resolutions.

Community
  • 1
  • 1
tmdavison
  • 64,360
  • 12
  • 187
  • 165
0

Because plt.savefig renders a picture from your data and has an option dpi, dots per inch, it is set to some default value. You can change the quality of your figure by doing plt.savefig('myfig.png', dpi=100)

Sleepyhead
  • 1,009
  • 1
  • 10
  • 27
-1

Genuine Matplotlib Engine is highly abstract, while Renderers ...

As an initial remark, matplotlib toolbox helps to create a lot of 2D / 3D plotting with also having a support for composing smart overlays from pixmap-DataSET-s

( "pictures" could be imagined as a {3D[for RGB] | 4D[for RGBA] }-colourspace data in pixmap-DataSet-s with 2D-[x,y]-mapping of colours onto "2D-paper" )

So one can overlay / view / save pixmap-DataSET pictures via matplotlib methods.


How to make pixmap size settings then?

For "picture" objects, that are rather "special" to the unconstrained world of unlimited numerical precision and any-depth LevelOfDetail in matplotlib, there are a few settings, that come into account at the very end of the processing lifecycle - at the output-generation moment.

So,
matplotlib can instruct it's (pre)selected Renderer
to produce final graphical output
at a special typographic density ... aka dpi = 60 dots-per-inch
and
also give an overall sizing ... figsize = ( 1, 1 ) which makes your 60 x 60 image exactly 60x60 pixels ( once you spend a bit more effort to enforce matplotlib to disable all edges and other layout-specific surroundings )


The overlay composition may also use .figimage() method, where one can additionally specify x0 = x_PX_OFFSET and y0 = y_PX_OFFSET details about where to start placing a picture data within a given figsize * dpi pixel-mapping area.

user3666197
  • 1
  • 6
  • 50
  • 92