matplotlib 如何在顶部添加图像斧和拉伸,以适应垂直/水平[关闭]

ux6nzvsh  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(109)

**已关闭。**此问题需要debugging details。目前不接受答案。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
上个月关门了。
Improve this question
我有一个Map投影和一个梯度图像(大的)。我想把这张图片添加到Map的顶部,然后拉伸,这样它就可以放进上面的一个盒子里(小的那个).我有它们的大小和坐标,因为我用Map本身绘制它们。我怎么做呢?我看到一些选项使用OffsetImage,但它似乎没有选择选项?图像本身是正方形的,但由于Map投影,它们必须被扭曲。

puruo6ea

puruo6ea1#

基本示例:

import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import numpy as np

# Sample data
lons = np.linspace(-180, 180, 1000)
lats = np.linspace(-90, 90, 500)
data = np.random.rand(len(lats), len(lons))  # dummy data for the map

fig, ax = plt.subplots()
ax.imshow(data, extent=[-180, 180, -90, 90], origin='lower')

# Assuming you have your image loaded as a numpy array
img = plt.imread('gradient_image.png')

# Box coordinates, for instance:
box_bottom_left = (-150, -60)
box_top_right = (-100, -30)
box_width = box_top_right[0] - box_bottom_left[0]
box_height = box_top_right[1] - box_bottom_left[1]

# Adjust the image dimensions to fit the bounding box aspect ratio
adjusted_image = plt.imshow(img).set_data(img)
adjusted_image.set_extent([box_bottom_left[0], box_top_right[0], box_bottom_left[1], box_top_right[1]])

# Add the adjusted image to the map
imagebox = OffsetImage(adjusted_image, zoom=1)
ab = AnnotationBbox(imagebox, (box_bottom_left[0], box_bottom_left[1]), frameon=False, boxcoords="data", pad=0)

ax.add_artist(ab)
plt.show()

相关问题