使用 matplotlib 添加背景图像时如何避免改变纵横比?
How to avoid change of aspect when adding a background image with matplotlib?
我想使用 matplotlib 获取一些带有背景图像的数据。
我发现 here 我需要使用 extent
。
但是,如果我的数据范围不同,则绘图比例如下。
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])
如何保持轴的限制并避免缩放?
Matplotlibs imshow
自动设置轴的纵横比。您可以通过设置 aspect
参数来更改此行为。
ax.imshow(img, zorder=0, extent=[0.5, 8.0, 0.0, 1.0], aspect='auto')
您可以在 documentation 中找到更多详细信息。
我想使用 matplotlib 获取一些带有背景图像的数据。
我发现 here 我需要使用 extent
。
但是,如果我的数据范围不同,则绘图比例如下。
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])
如何保持轴的限制并避免缩放?
Matplotlibs imshow
自动设置轴的纵横比。您可以通过设置 aspect
参数来更改此行为。
ax.imshow(img, zorder=0, extent=[0.5, 8.0, 0.0, 1.0], aspect='auto')
您可以在 documentation 中找到更多详细信息。