本文介绍了大 pandas Mapplotlib,如何在没有轮廓的情况下围绕任何形状进行绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I run the code below in a Jupyter Notebook,I get a map of the world, colored in red.

国家之间有细微的白线.有没有办法绘制世界,让所有国家是牢固的,两者之间没有界限?

There are fine white-ish lines between the countries.Is there a way to plot the world so that all countriesare solid and there's no line in between?

我问,因为我的现实用例是一个很好的网格,行为就像世界地图:每个网格形状都有一个精美的轮廓我不想在情节中有. (更新,因为有人问:网格形状将不会具有相同的填充颜色.

I'm asking, because my real world usecase is a fine grid thatbehaves just like the world map: Each grid shape has a fine outlinewhich I do not want to have in the plot. (Update, since this was asked: The grid shapes will not have the same fill color.

)

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 一个显示问题的简化示例.

For the grid example, the grid files are at https://opendata-esri-de.opendata.arcgis.com/datasets/3c1f46241cbb4b669e18b002e4893711_0A simplified example that shows the problem.

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)

推荐答案

白线归因于抗锯齿.这通常会使视觉更平滑,但会导致不同形状之间出现白线.您可以通过以下方式关闭锯齿处理

The white lines are due to antialiasing. This usually makes the visual more smooth, but leads to white lines in between different shapes. You can turn off anialiasing via

antialiased=False

这有不可避免的缺点,那就是情节看起来像是像素化的.

That has the inevitable drawback of the plot looking pixelated.

另一种方法是使补丁具有一定线宽的边缘.边缘的颜色应该与面孔的颜色相同,所以

An alternative is to give the patches an edge with a certain linewidth. The edges should probably have the same color as the faces, so

edgecolor="face", linewidth=0.4

将是一个选择.这样可以消除白线,但是会产生轻微的摩擦"效果(您会注意到主要是在印度尼西亚或日本这样的岛屿上进行的观察).这将是更引人注意的,特征越小,因此与显示六边形图可能无关.不过,稍微增加一下线宽可能会进一步改善结果.

would be an option. This removes the white lines, but introduces a slight "searing" effect (You'll notice mainly looking at islands like Indonesia or Japan). This will be the more noticable, the smaller the features, so it may be irrelevant for showing a hexbin plot. Still, playing a bit with the linewidth might improve the result further.

复制代码:

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()

这篇关于大 pandas Mapplotlib,如何在没有轮廓的情况下围绕任何形状进行绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:16