How to get pixel coordinates for Matplotlib-generated scatterplot?

When working with matplotlib scatterplots, you might need to convert data coordinates to pixel coordinates for UI interactions or precise positioning. This can be achieved using matplotlib's coordinate transformation system.

Understanding Coordinate Systems

Matplotlib uses different coordinate systems −

  • Data coordinates − The actual x, y values of your data points
  • Pixel coordinates − Screen/display coordinates in pixels
  • Transform objects − Convert between coordinate systems

Getting Pixel Coordinates from Scatterplot

Here's how to extract pixel coordinates from a matplotlib scatterplot ?

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create sample data
n = 10
fig, ax = plt.subplots()

# Create scatter plot (using plot with marker style)
points, = ax.plot(np.random.random(n), np.random.random(n), 'r*')

# Get the data coordinates
x, y = points.get_data()

# Transform data coordinates to pixel coordinates
pixels = ax.transData.transform(np.vstack([x, y]).T)
x_pixels, y_pixels = pixels.T

# Get figure dimensions
width, height = fig.canvas.get_width_height()

# Flip y-coordinates (matplotlib origin is bottom-left, display origin is top-left)
y_pixels = height - y_pixels

print("The pixel coordinates are:")
for xp, yp in zip(x_pixels, y_pixels):
    print('{x:0.2f}\t{y:0.2f}'.format(x=xp, y=yp))

plt.show()

The output will show both the plot and pixel coordinates ?

The pixel coordinates are:
564.93   161.83
446.27   112.60
153.39   247.65
236.90   258.34
519.10   301.04
237.66   118.16
149.71   303.29
386.74   105.81
172.93   121.81
110.94   116.20

Alternative Method Using Scatter

You can also use the scatter() method directly ?

import numpy as np
import matplotlib.pyplot as plt

# Generate sample data
x_data = np.random.random(8)
y_data = np.random.random(8)

fig, ax = plt.subplots(figsize=(7, 4))

# Create scatter plot
scatter = ax.scatter(x_data, y_data, c='blue', s=50)

# Transform to pixel coordinates
data_coords = np.column_stack([x_data, y_data])
pixel_coords = ax.transData.transform(data_coords)

print("Data coordinates ? Pixel coordinates:")
for i, (data_pt, pixel_pt) in enumerate(zip(data_coords, pixel_coords)):
    print(f"Point {i+1}: ({data_pt[0]:.3f}, {data_pt[1]:.3f}) ? ({pixel_pt[0]:.1f}, {pixel_pt[1]:.1f})")

plt.title("Scatter Plot with Pixel Coordinates")
plt.show()
Data coordinates ? Pixel coordinates:
Point 1: (0.123, 0.456) ? (126.2, 301.5)
Point 2: (0.789, 0.234) ? (542.1, 252.8)
Point 3: (0.345, 0.678) ? (267.3, 378.9)
Point 4: (0.567, 0.890) ? (398.4, 445.2)
Point 5: (0.901, 0.123) ? (625.7, 189.1)
Point 6: (0.234, 0.567) ? (198.8, 356.4)
Point 7: (0.678, 0.345) ? (471.9, 278.3)
Point 8: (0.456, 0.789) ? (345.2, 423.6)

Key Points

  • ax.transData.transform() converts data coordinates to display coordinates
  • Y-coordinates need flipping because matplotlib's origin is bottom-left
  • fig.canvas.get_width_height() provides figure dimensions in pixels
  • This technique works for any matplotlib plot type, not just scatter plots

Conclusion

Use ax.transData.transform() to convert data coordinates to pixel coordinates in matplotlib. Remember to flip the y-coordinates for display coordinate systems that have their origin at the top-left.

---
Updated on: 2026-03-26T02:31:54+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements