1

I am plotting a numpy array with matplotlib (imshow function). Its size is 1000*3000 and its values are floats between 0 and around 2000.

When the maximum values are around 10 (it was just for a test) I have correct values on my figure, but with that amplitude, all values are rounded to int so I cannot see the nuance in some part of my array.

Is there a parameter in imshow corresponding to that problem ? I thought to the second (cmap=None) but I didn't succeed.

Here is an example :

import numpy as np
import matplotlib.pyplot as plt
import random as rand

data = np.zeros((10,10))

for i in range(10):
    for j in range(10):
        data[i,j] = 900 + 10*i + rand.random()

plt.imshow(data)
plt.show()

The values in the matplotlib window are all integer whereas in the real array they are not.

jrjc
  • 21,103
  • 9
  • 64
  • 78
trapuck
  • 43
  • 7
  • 1
    A [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) would be helpful. – DavidG Apr 07 '17 at 13:11

1 Answers1

1

You can modify your coordinate formatter. An official example is shown here. Also, the first part of the answer to this question shows a slightly different way of doing the same thing. I have modified the code so that the x and y coordinates are integers while the z is a float with 4 decimal places:

class Formatter(object):
    def __init__(self, im):
        self.im = im
    def __call__(self, x, y):
        z = self.im.get_array()[int(y), int(x)]
        return 'x=%i, y=%i, z=%1.4f' % (x, y, z)

data = np.zeros((10, 10))

for i in range(10):
    for j in range(10):
        data[i, j] = 900 + 10 * i + rand.random()

fig, ax = plt.subplots()
im = ax.imshow(data)
ax.format_coord = Formatter(im)

plt.show()
Community
  • 1
  • 1
DavidG
  • 24,279
  • 14
  • 89
  • 82