0

I have got a figure with with an axis object. I would like to use ax.scatter to scatter something into the plot, which is no problem. On top I want to draw 2D array data into the same figure (using ax.imshow for example...):

import numpy as np
from matplotlib import pyplot as plt

vidRes = np.array([[-700, 700], [-500,500]])
dim = np.array([vidRes[0,1] - vidRes[0,0],vidRes[1,1] - vidRes[1,0]])

with plt.style.context('dark_background'):
    fg = plt.figure()
    fg.set_size_inches((10, 10*dim[1]/dim[0]))
    ax = fg.add_subplot(111)
    ax.set_aspect('equal')
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    ax.set_xlim(vidRes[0,0], vidRes[0,1])
    ax.set_ylim(vidRes[1,0], vidRes[1,1])

random = np.random.random(size = (401, 401,),)
ax.imshow(random, cmap = 'hot')

This is the output

The 2D array has 401x401 entries. Is there a straight forward way to move the imshow object to center it on a given pixel in my 1400x1000 pixel figure? Am I blind to not see it?

The image is drawn into the figure but it is always aligned with the lower left or upper left corner (depending on origin of ax.imshow) to the (0,0) coordinate at the center of my plot, I can't move it anywhere else.

1 Answer 1

1

You can use the extent argument. From the docs:

extent : scalars (left, right, bottom, top), optional, default: None The location, in data-coordinates, of the lower-left and upper-right corners. If None, the image is positioned such that the pixel centers fall on zero-based (row, column) indices.

Changing your imshow call to the following line:

ax.imshow(random, cmap = 'hot', extent=[-200.5, 200.5, -200.5, 200.5])

yields:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot, I somehow didn't get it right it seems. I used extent with pixel coordinates before but since I am plotting on a micrometer basis that just put my imshow plot somwhere next to infinity... I solved my problem by using the imshow extent parameter where I just put the coordinates of the left, right, bottom and top points in in meters. So I didn't use pixel coordinates (e. g. position (500,323) in a (1000x1000) image) but just the very same meter values that I use in my scatter plots, in case anyone is wondering what values to put in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.