Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3678922
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:23:56+00:00 2026-05-19T03:23:56+00:00

I use matplotlib to display a matrix of numbers as an image, attach labels

  • 0

I use matplotlib to display a matrix of numbers as an image, attach labels along the axes, and save the plot to a PNG file. For the purpose of creating an HTML image map, I need to know the pixel coordinates in the PNG file for a region in the image being displayed by imshow.

I have found an example of how to do this with a regular plot, but when I try to do the same with imshow, the mapping is not correct. Here is my code, which saves an image and attempts to print the pixel coordinates of the center of each square on the diagonal:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axim = ax.imshow(np.random.random((27,27)), interpolation='nearest')
for x, y in  axim.get_transform().transform(zip(range(28), range(28))):
    print int(x), int(fig.get_figheight() * fig.get_dpi() - y)
plt.savefig('foo.png', dpi=fig.get_dpi())

Here is the resulting foo.png, shown as a screenshot in order to include the rulers:

Screenshot of foo.png with rulers

The output of the script starts and ends as follows:

73 55
92 69
111 83
130 97
149 112
…
509 382
528 396
547 410
566 424
585 439

As you see, the y-coordinates are correct, but the x-coordinates are stretched: they range from 73 to 585 instead of the expected 135 to 506, and they are spaced 19 pixels o.c. instead of the expected 14. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:23:56+00:00Added an answer on May 19, 2026 at 3:23 am

    This is one of the more confusing parts of trying to get exact pixel values from matplotlib. Matplotlib separates the renderer that handles exact pixel values from the canvas that the figure and axes are drawn on.

    Basically, the renderer that exists when the figure is initially created (but not yet displayed) is not necessarily the same as the renderer that is used when displaying the figure or saving it to a file.

    What you’re doing is correct, but it’s using the initial renderer, not the one that’s used when the figure is saved.

    To illustrate this, here’s a slightly simplified version of your code:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    im = ax.imshow(np.random.random((27,27)), interpolation='nearest')
    
    for i in range(28):
        x, y =  ax.transData.transform_point([i,i])
        print '%i, %i' % (x, fig.bbox.height - y)
    
    fig.savefig('foo.png', dpi=fig.dpi)
    

    This yields similar results to what you have above: (the differences are due to different rendering backends between your machine and mine)

    89, 55
    107, 69
    125, 83
    ...
    548, 410
    566, 424
    585, 439
    

    However, if we do the exact same thing, but instead draw the figure before displaying the coordinates, we get the correct answer!

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    im = ax.imshow(np.random.random((27,27)), interpolation='nearest')
    
    fig.canvas.draw()
    
    for i in range(28):
        x, y =  ax.transData.transform_point([i,i])
        print '%i, %i' % (x, fig.bbox.height - y)
    
    fig.savefig('foo.png', dpi=fig.dpi)
    

    This yields: (Keep in mind that the edge of the figure is at <-0.5, -0.5> in data coordinates, not <0, 0>. (i.e. the coordinates for the plotted image are pixel-centered) This is why <0, 0> yields 143, 55, and not 135, 48)

    143, 55
    157, 69
    171, 83
    ...
    498, 410
    512, 424
    527, 439
    

    Of course, drawing the figure just to draw it again when it’s saved is redundant and computationally expensive.

    To avoid drawing things twice, you can connect a callback function to the draw event, and output your HTML image map inside this function. As a quick example:

    import numpy as np
    import matplotlib.pyplot as plt
    
    def print_pixel_coords(event):
        fig = event.canvas.figure
        ax = fig.axes[0] # I'm assuming there's only one subplot here...
        for i in range(28):
            x, y = ax.transData.transform_point([i,i])
            print '%i, %i' % (x, fig.bbox.height - y)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    im = ax.imshow(np.random.random((27,27)), interpolation='nearest')
    
    fig.canvas.mpl_connect('draw_event', print_pixel_coords)
    
    fig.savefig('foo.png', dpi=fig.dpi)
    

    Which yields the correct output, while only drawing the figure once, when it is saved:

    143, 55
    157, 69
    171, 83
    ...
    498, 410
    512, 424
    527, 439
    

    Another advantage is that you can use any dpi in the call to fig.savefig without having to manually set the fig object’s dpi beforehand. Therefore, when using the callback function, you can just do fig.savefig('foo.png'), (or fig.savefig('foo.png', dpi=whatever)) and you’ll get output that matches the saved .png file. (The default dpi when saving a figure is 100, while the default dpi for a figure object is 80, which is why you had to specify the dpi to be the same as fig.dpi in the first place)

    Hopefully that’s at least somewhat clear!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a temp directory that will be unique to this build.
(please excuse that I didn't use aliases). I would like my query output to
My question is about memory use and objects in actionscript 2. If I have
I make a distributed embedded application that will make use of several micro-controllers. The
I need to develop a file indexing application in python and wanted to know
I need to solve the following question which i can't get to work by
I am learning on my own about writing an interpreter for a programming language,
I have a new web app that is packaged as a WAR as part
We manage a site for a medical charity. They have a number of links
I've found several jQuery syntaxes for nullifying the enter on a form. First one:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.