Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用斜线填充的图例绘制geopandas_Python_Pandas_Matplotlib_Geopandas - Fatal编程技术网

Python 用斜线填充的图例绘制geopandas

Python 用斜线填充的图例绘制geopandas,python,pandas,matplotlib,geopandas,Python,Pandas,Matplotlib,Geopandas,我用不同的颜色和图案在PA地图上显示了三个县。中心县用斜线表示,斜线使用hatch='\\'。但我很难在传说中展示这样的图案 我知道这是行不通的,但我尝试了Line2D([0],[0],color='white',hatch='\\',lw=4,label='center County'),但出现了类似“hatch不是属性”的错误 创建多边形时,属性facecolor定义填充颜色。要为多边形特征创建正确的图例,需要使用mpatches.Patch 下面的代码演示了如何使用facecolor和mp

我用不同的颜色和图案在PA地图上显示了三个县。中心县用斜线表示,斜线使用
hatch='\\'
。但我很难在传说中展示这样的图案

我知道这是行不通的,但我尝试了
Line2D([0],[0],color='white',hatch='\\',lw=4,label='center County')
,但出现了类似“hatch不是属性”的错误


创建多边形时,属性
facecolor
定义填充颜色。要为多边形特征创建正确的图例,需要使用mpatches.Patch

下面的代码演示了如何使用
facecolor
mpatches.Patch

import geopandas as gpd
import matplotlib.pyplot as plt
#from matplotlib.lines import Line2D
import matplotlib.patches as mpatches
from cartopy import crs as ccrs

#fig, ax = plt.subplots(1,figsize=(8,8))
fig, ax = plt.subplots(figsize=(9,9), subplot_kw={'projection': ccrs.PlateCarree()})

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))

asia = world[(world.continent == "Asia")]  #take Asia countries
asia.plot(ax=ax, color="lightgreen")

china = asia[(asia.name == "China")]
india = asia[(asia.name == "India")]
saudi = asia[(asia.name == "Saudi Arabia")]


ax.add_geometries(china['geometry'], crs=ccrs.PlateCarree(), \
                      facecolor='w', hatch='\\\\\\\\', edgecolor='k', label='China')
ax.add_geometries(india['geometry'], crs=ccrs.PlateCarree(), \
                      color='grey', label='India')
ax.add_geometries(saudi['geometry'], crs=ccrs.PlateCarree(), \
                      color='red', label='Saudi Arabia')

LegendElement = [
                 mpatches.Patch(facecolor='w', hatch='\\\\\\\\', edgecolor='k', label='China'),
                 mpatches.Patch(color='grey', label='India'),
                 mpatches.Patch(color='red', label='Saudi Arabia')
                ]
ax.legend(handles = LegendElement, loc='upper right')
plt.show()
输出图如下所示:

import geopandas as gpd
import matplotlib.pyplot as plt
#from matplotlib.lines import Line2D
import matplotlib.patches as mpatches
from cartopy import crs as ccrs

#fig, ax = plt.subplots(1,figsize=(8,8))
fig, ax = plt.subplots(figsize=(9,9), subplot_kw={'projection': ccrs.PlateCarree()})

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))

asia = world[(world.continent == "Asia")]  #take Asia countries
asia.plot(ax=ax, color="lightgreen")

china = asia[(asia.name == "China")]
india = asia[(asia.name == "India")]
saudi = asia[(asia.name == "Saudi Arabia")]


ax.add_geometries(china['geometry'], crs=ccrs.PlateCarree(), \
                      facecolor='w', hatch='\\\\\\\\', edgecolor='k', label='China')
ax.add_geometries(india['geometry'], crs=ccrs.PlateCarree(), \
                      color='grey', label='India')
ax.add_geometries(saudi['geometry'], crs=ccrs.PlateCarree(), \
                      color='red', label='Saudi Arabia')

LegendElement = [
                 mpatches.Patch(facecolor='w', hatch='\\\\\\\\', edgecolor='k', label='China'),
                 mpatches.Patch(color='grey', label='India'),
                 mpatches.Patch(color='red', label='Saudi Arabia')
                ]
ax.legend(handles = LegendElement, loc='upper right')
plt.show()