【问题标题】:MPL Basemap Projections not showing output properlyMPL 底图投影未正确显示输出
【发布时间】:2023-02-19 21:05:58
【问题描述】:

所以我从我的老师那里得到了这段旧代码,它使用底图绘制了一个 Mollweide 投影

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

from itertools import chain

def draw_map(m, scale=0.2):
  # draw a shaded-relief image
  im1 = m.shadedrelief(scale=scale)
  # print(im1, vars(im1))
  # print(f'{dir(im1) = }')
  # lats and longs are returned as a dictionary
  lats = m.drawparallels(np.linspace(-90, 90, 13))
  lons = m.drawmeridians(np.linspace(-180, 180, 13))
  # keys contain the plt.Line2D instances
  lat_lines = chain(*(tup[1][0] for tup in lats.items()))
  lon_lines = chain(*(tup[1][0] for tup in lons.items()))
  all_lines = chain(lat_lines, lon_lines)
  # cycle through these lines and set the desired style
  for line in all_lines:
    line.set(linestyle='-', alpha=0.3, color='w')


fig = plt.figure(figsize=(8, 6), edgecolor='w')
m = Basemap(projection='moll', resolution=None,
            lat_0=0, lon_0=0)

draw_map(m)

当我试图绘制它时,我得到了没有地球背景图像的空白输出:

预期输出:

据我搜索,我找不到这个问题的解决方案

据我所见,底图中的每个地理投影方法都会发生此问题,例如bluemarbleshadedrelief

我在 VS Code 的 Jupyter Notebook 中使用 Python v3.11.0 运行这段代码

我在 Google Colab 中也得到了相同的输出,安装了 mpltoolkits 和底图

即使我在单独的 Python 文件中运行它也有相同的输出

【问题讨论】:

    标签: python-3.x jupyter-notebook data-science matplotlib-basemap map-projections


    【解决方案1】:

    没关系,这是一个简单的修复

    我只需要在 draw_map 函数中添加这两行就可以像这样更新它:

    from itertools import chain
    
    def draw_map(m, scale=0.2):
      # draw a shaded-relief image
    
      # These 2 lines
      im1 = m.shadedrelief(scale=scale) # Value stored in a variable to resolve a bug
      im1.axes.add_image(im1) # The line that resolves the "No BG Image" bug
    
      # lats and longs are returned as a dictionary
      lats = m.drawparallels(np.linspace(-90, 90, 13))
      lons = m.drawmeridians(np.linspace(-180, 180, 13))
      # keys contain the plt.Line2D instances
      lat_lines = chain(*(tup[1][0] for tup in lats.items()))
      lon_lines = chain(*(tup[1][0] for tup in lons.items()))
      all_lines = chain(lat_lines, lon_lines)
      # cycle through these lines and set the desired style
      for line in all_lines:
        line.set(linestyle='-', alpha=0.3, color='w')
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 2014-07-20
      • 2016-09-08
      • 2013-05-15
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多