3

I have code that produces a plot based on the example below. I would like to force the outputted plot to be a 128x128 square if possible instead of 64x128. The data I have is intended to be viewed as a square even though its matrix does not exhibit NxN properties.

Thank you.

import numpy as np
import pylab as pl

my_matrix = []
for x in range(128):
    row = []
    for value in range(64):
        row.append( float(value) / 63 )
    my_matrix.append(row)

array = np.matrix(my_matrix)
pl.axes()
pl.imshow(array, interpolation='none', cmap='jet', origin='lower')
pl.colorbar(shrink=0.95)
pl.xticks(())
pl.yticks(())
pl.show()
Matt
  • 3,592
  • 5
  • 21
  • 26

1 Answers1

1

You can define the axes aspect ratio as follows:

pl.axes().set_aspect(0.5)

The factor 0.5 compensates the aspect ration 128:64 = 2:1 of your data.

For further information you might look into this answer.

Community
  • 1
  • 1
Falko
  • 17,076
  • 13
  • 60
  • 105
  • Do you also know how I can set my colorbar() ticks such that [-18, -14, -12, -10, -8, -6, -4, -2] where -18 is at the bottom and -2 is at the top. Also -18 tick correlates to values closest to zero and -2 tick correlates to values closest to 1. – Matt Aug 22 '14 at 22:08
  • 1
    @Matt: Have a look into the [```clim``` argument](http://matplotlib.org/api/cm_api.html#matplotlib.cm.ScalarMappable.set_clim), e.g. ```pl.imshow(..., clim=[-18,-2])```. This way you can limit the color range. But it should correspond to the data, so you might scale them accordingly: ```pl.imshow(array * (-18), ..., clim=[-18,-2])```. – Falko Aug 22 '14 at 22:16
  • Ok Thanks. I essentially have a dataset which represents different orders of magnitude. from `10E-18` to `zero` in increments of `10E-2`. I'm a total noob to `matplotlib` so I appreciate it! – Matt Aug 22 '14 at 22:24