1

I want to some data with a background image using matplotlib.

I found here that I need to use extent.

However, if the range of my data is not the same the plot scales as follows.

import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imread
fig = plt.figure(figsize=(8,8))
ax = plt.axes(xlim=(0,10),ylim=(0,1))
np.random.seed(0)
x = np.random.uniform(0.0,10.0,15)
y = np.random.uniform(0.0,1.0,15)
img = imread('lena.jpg')
ax.scatter(x,y,zorder=1)
ax.imshow(img, zorder=0, extent=[0.5, 8.0, 0.0, 1.0])

How can I keep the limits of my axes and avoid the scaling?

enter image description here

Community
  • 1
  • 1
ilciavo
  • 3,069
  • 7
  • 26
  • 40

1 Answers1

2

Matplotlibs imshow sets the aspect ratio of the axes automatically. You can change this behaviour by setting the aspect argument.

ax.imshow(img, zorder=0, extent=[0.5, 8.0, 0.0, 1.0], aspect='auto')

You'll find more detailed information in the documentation.

hitzg
  • 12,133
  • 52
  • 54