簡體   English   中英

如何在matplotlib.pyplot.imshow中使用“范圍”

[英]how to use 'extent' in matplotlib.pyplot.imshow

我設法繪制了數據,並想為其添加背景圖像(地圖)。 數據是按長/緯度值繪制的,我也具有圖像的三個角(左上角,右上角和左下角)的長/緯度值。

我試圖弄清楚如何在imshow中使用'extent'選項。 但是,我發現的示例並未說明如何為每個角指定x和y(在我的情況下,我具有三個角的信息)。

將圖像添加到繪圖中時,如何為圖像指定三個角的位置?

謝謝

在當前軸的坐標中指定要粘貼圖像的矩形的角

范圍定義了左右限制,以及上下限制。 它采用四個類似的值: extent=[horizontal_min,horizontal_max,vertical_min,vertical_max]

假設您在水平軸上有經度,請使用extent=[longitude_top_left,longitude_top_right,latitude_bottom_left,latitude_top_left] longitude_top_left和longitude_bottom_left應該相同,latitude_top_left和latitude_top_right應該相同,並且這些對中的值可以互換。

如果圖像的第一個元素應繪制在左下方,則也應使用origin='lower' lower'imshow選項,否則,您需要使用'upper'默認值。

這是一個基於http://matplotlib.org/examples/pylab_examples/image_demo3.html的示例,顯示了范圍的使用。

#!/usr/bin/env python
from pylab import *
try:
    from PIL import Image
except ImportError, exc:
    raise SystemExit("PIL must be installed to run this example")

import matplotlib.cbook as cbook

datafile = cbook.get_sample_data('ada.png')
h = Image.open(datafile)
dpi = rcParams['figure.dpi']
figsize = h.size[0]/dpi, h.size[1]/dpi

figure(figsize=figsize)
ax = axes([0,0,1,1], frameon=False)
ax.set_axis_off()
ax.set_xlim(0,2)
ax.set_ylim(0,2)
im = imshow(h, origin='upper',extent=[-2,4,-2,4])  # axes zoom in on portion of image
im2 = imshow(h, origin='upper',extent=[0,.5,0,.5]) # image is a small inset on axes

show()

如果您未設置軸限制,則它們將成為您的范圍,然后似乎沒有任何作用。

Ada Lovelace圖像與插圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM