matplotlib - Geopandas & Mapplotlib,如何在没有轮廓的情况下绘制任何形状?

标签 matplotlib geopandas

当我在 Jupyter Notebook 中运行以下代码时,
我得到了一张世界地图,颜色为红色。

enter image description here

国家之间有细白线。
有没有办法绘制世界,让所有国家
是坚实的,中间没有界限吗?

我在问,因为我的真实世界用例是一个精细的网格,
就像世界地图一样:每个网格形状都有一个精细的轮廓
我不想在情节中出现。 (更新,因为有人问过:网格形状不会有相同的填充颜色。

grid example )

import geopandas as gpd
import geoplot as gplt
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world['total'] = 1
world.plot(column='total', cmap='Set1')

对于网格示例,网格文件位于 https://opendata-esri-de.opendata.arcgis.com/datasets/3c1f46241cbb4b669e18b002e4893711_0
显示问题的简化示例。
sf = 'Hexagone_125_km/Hexagone_125_km.shp'
shp = gpd.read_file(sf)
shp.crs = {'init': 'epsg:4326'}
shp['sum'] = 1  # for example, fill sum with something
shp.plot(figsize=(20,20), column='sum', cmap='gnuplot', alpha=1, legend=True)

最佳答案

白线是由于抗锯齿。这通常会使视觉更平滑,但会导致不同形状之间出现白线。您可以通过以下方式关闭抗锯齿功能

antialiased=False

这具有情节看起来像素化的不可避免的缺点。

另一种方法是给补丁一个具有一定线宽的边缘。边缘可能应该与面具有相同的颜色,所以
edgecolor="face", linewidth=0.4

将是一个选择。这消除了白线,但引入了轻微的“灼热”效果(您会注意到主要是在看印度尼西亚或日本等岛屿)。这将越明显,特征越小,因此它可能与显示六边形图无关。不过,稍微调整一下线宽可能会进一步改善结果。

enter image description here

复制代码:
import numpy as np; np.random.seed(42)
import geopandas as gpd
import matplotlib.pyplot as plt

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world['total'] = np.random.randint(0,10, size=len(world))

fig, (ax1, ax2, ax3) = plt.subplots(nrows=3, figsize=(7,10))

world.plot(column='total', cmap='Set1', ax=ax1)

world.plot(column='total', cmap='Set1', ax=ax2, antialiased=False)

world.plot(column='total', cmap='Set1', ax=ax3, edgecolor="face", linewidth=0.4)


ax1.set_title("original")
ax2.set_title("antialiased=False")
ax3.set_title("edgecolor='face', linewidth=0.4")
plt.tight_layout()
plt.savefig("world.png")
plt.show()

关于matplotlib - Geopandas & Mapplotlib,如何在没有轮廓的情况下绘制任何形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55759070/

相关文章:

python - 从物理公式(无数据文件)、python 和 numpy 绘制图形

python - 使用交互式平移和缩放制作 Altair map

python - 在 GeoDataFrame 中查找不重叠的多边形

python - Matplotlib basemap Hexbin 动画 : Clear hexbins between frames

python - 在seaborn tsplot中将图例移到图外

pandas - 合并 geopandas 中的地理数据框(CRS 不匹配)

python - Geopandas 设置几何图形 : ValueError for MultiPolygon "equal len keys and value"

python-3.x - 如何实现rtree来计算相交面积?

python - Matplotlib 中的垂直偏移刻度标签

python - 设置 pandas 数据框的格式