576i
576i

Reputation: 8372

Geopandas & Mapplotlib, how do I plot without an outline around any shape?

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

enter image description here

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

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

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

For the grid example, the grid files are at https://opendata-esri-de.opendata.arcgis.com/datasets/3c1f46241cbb4b669e18b002e4893711_0 A 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)

Upvotes: 8

Views: 8240

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339430

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.

enter image description here

Code for reproduction:

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

Upvotes: 15

Related Questions