【问题标题】:Map dimensions change when highlighting a country with Geopandas使用 Geopandas 突出显示国家时地图尺寸会发生变化
【发布时间】:2022-09-24 00:28:32
【问题描述】:

我在 python 中使用 Geopandas 和 matplotlib 创建了一张世界地图。当我尝试突出显示某个国家/地区时,地图尺寸会发生变化。如何保留地图尺寸?

import matplotlib
from matplotlib import pyplot
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.collections import PatchCollection
from matplotlib.figure import Figure
import geopandas as gpd
import pandas

self.figure = Figure()
self.canvas = FigureCanvas(self, -1, self.figure)
self.axes = self.figure.add_axes([0, 0, 1, 1])
self.axes.margins(0.0)
self.world_data = gpd.read_file(WORLD)

self.axes.clear()
self.axes.axis(\'off\')
self.figure.set_facecolor(WATER)


self.map_plot = self.world_data.to_crs(epsg=4326).plot(ax=self.axes, color=LAND)

if country_highlight:
            self.world_data[self.world_data.ISO_A2_EH ==country_code].plot(edgecolor=u\'gray\', color=\'#fa8a48\', ax=self.map_plot)

self.canvas.draw()

    标签: python pandas matplotlib geopandas


    【解决方案1】:

    如果您想在某个点之后保持轴不变的地图范围,您可以保存它们并在以后重新执行它们:

    # I'll drop references to `self` for the rest of the answer for clarity
    ax = self.axes
    world_data = self.world_data
    
    world_data.to_crs(epsg=4326).plot(ax=ax, color=LAND)
    
    ylim = ax.get_ylim()
    xlim = ax.get_xlim()
    
    if country_highlight:
        self.world_data[
            world_data.ISO_A2_EH == country_code
        ].plot(edgecolor=u'gray', color='#fa8a48', ax=ax)
    
    ax.set_ylim(*ylim)
    ax.set_xlim(*xlim)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-19
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      相关资源
      最近更新 更多