1

I'm currently trying to plot with matplotlib a 2d map recorded with an instrument. The instrument is moving 2 motors (it makes a raster) and records the associated intensity value. I'm currently able to plot the data and to associate the values I want to the axes, but I would like to digitize (make discrete) these values in order to obtain at each pixel of the image the corresponding values for the motors.

I'm currently using the following code (in the example I'll use x and y to define the motor positions):

import pylab as pl
pl.imshow(intensity, extent=(x_min, x_max, y_min, y_max),
          interpolation='none')

The code works quite well but if I select one of the pixel on my plot with the cursor, it returns continuous values with many digits (like in figure).

example_map

Would it be possible to obtain directly the values of the motors (which I have stored for each point/pixel) by positioning the cursor on them?

Thanks for the help,

Fabio

user2504163
  • 63
  • 2
  • 10
  • I can't help you with your cursor-based problem, but maaaaybe, [seaborns heatmap with annot=True](https://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.heatmap.html) is a valid alternative? In this case, these values are plotted directly. But maybe your grid-resolution is too high. – sascha May 10 '16 at 18:39
  • Thanks for the indication, these maps looks very nice, but as you have anticipated the grid resolution in my case is too high. – user2504163 May 11 '16 at 13:35

1 Answers1

0

You can do it by modifying the coordinate formatter like in this example on the matplotlib documentation. A simple adaptation to your request is:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

X = 10*np.random.rand(5, 3)

fig, ax = plt.subplots()
ax.imshow(X, cmap=cm.jet, interpolation='nearest')

def format_coord(x, y):
    return 'x=%i, y=%i' % (x+1, y+1)

ax.format_coord = format_coord
plt.show()

, which will result in this:

Changing the format of mouse position in matplotlib window

Also you might want to check out mpldatacursor for something more pretty. For this option take a look at this question here in SO.

Community
  • 1
  • 1
armatita
  • 12,825
  • 8
  • 48
  • 49