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

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

**已关闭。**此问题需要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#

基本示例:

  1. import matplotlib.pyplot as plt
  2. from matplotlib.offsetbox import OffsetImage, AnnotationBbox
  3. import numpy as np
  4. # Sample data
  5. lons = np.linspace(-180, 180, 1000)
  6. lats = np.linspace(-90, 90, 500)
  7. data = np.random.rand(len(lats), len(lons)) # dummy data for the map
  8. fig, ax = plt.subplots()
  9. ax.imshow(data, extent=[-180, 180, -90, 90], origin='lower')
  10. # Assuming you have your image loaded as a numpy array
  11. img = plt.imread('gradient_image.png')
  12. # Box coordinates, for instance:
  13. box_bottom_left = (-150, -60)
  14. box_top_right = (-100, -30)
  15. box_width = box_top_right[0] - box_bottom_left[0]
  16. box_height = box_top_right[1] - box_bottom_left[1]
  17. # Adjust the image dimensions to fit the bounding box aspect ratio
  18. adjusted_image = plt.imshow(img).set_data(img)
  19. adjusted_image.set_extent([box_bottom_left[0], box_top_right[0], box_bottom_left[1], box_top_right[1]])
  20. # Add the adjusted image to the map
  21. imagebox = OffsetImage(adjusted_image, zoom=1)
  22. ab = AnnotationBbox(imagebox, (box_bottom_left[0], box_bottom_left[1]), frameon=False, boxcoords="data", pad=0)
  23. ax.add_artist(ab)
  24. plt.show()
展开查看全部

相关问题