Creating Beautiful Maps with Python Beyond the defaults

A Beginner’s guide on customizing maps beyond the defaults

Photo by Jen Theodore on Unsplash

Making maps with Geopandas is very easy but I have to admit the defaults are not great as any other data visualization software out there. In this tutorial, we examine ways to change and create beautiful maps. There are many different ways to customize your maps. We cover specific ways you can enhance your map-making with parameters, selecting colours and adding context and base maps to your maps.

Let us make slick and aesthetically pleases maps with few and simple code.

Data and Default maps

Let us read the data and visualize it with the defaults. We use Geopandas read_file() function to read the data. Plotting geographic data with Geopandas is also easy as just calling plot()) function.

gdf = gpd.read_file(“data/malmo-pop.shp”)
gdf.plot()
Photo by Jen Theodore on Unsplash

Look at that map. We have to appreciate the ease of this despite being ugly. We can enhance and customize the map with parameters. Never accept defaults.

Customizing map parameters

The map above has several drawbacks we can easily fix with taking and customizing parameters. In this section, we review some basic elements that can help us make more aesthetically pleasing maps. Let us add some basic elements and customize the map.

fig, ax = plt.subplots(figsize=(16,16)) # 1
gdf.plot(ax=ax, color=”grey”, edgecolor=”white”, linewidth=0.2) # 2
plt.title(‘Mälmo Neighbourhoods’, fontsize=40, fontname=”Palatino 
Linotype”, color=”grey”) # 3
ax.axis(“off”) # 4
plt.axis('equal') # 5
plt.show() # 6
Photo by Jen Theodore on Unsplash

What have we done so far?. We have added a title for the map (#3) and removed the axis labels for the latitude and longitude (# 4). In (#5), we also make sure that the maps represent reality as much as possible by ensuring that scale ratio remains fixed — plt.axis("equal"). We have also increased the map size (# 1).

The actual colouring of the map is passed as parameters in plot() function. We changed the default blue colour of the map to grey and made the line edges smaller and white.

We have come a bit further to customize our map. The above map is more pleasant than the previous default map. In the next section, we get to know how to use colour effectively.

Use Colors effectively

Although the colour choice is personal, there are many tools that can help you choose colours effectively with design guides. One such tool is Palettable. These are some colour choices with some guidance from Palatable.

Photo by Jen Theodore on Unsplash

I am going to add street lines to the map so that we can choose different colours for the map.

streets = gpd.read_file(“data/malmo-streets.shp”)
colors = [“#A1E2E6”, “#E6BDA1”, “#B3A16B”, “#678072”, “#524A4A”]
palplot(colors, size=4)

And we can use these colours for the map passing in the function parameters.

fig, ax = plt.subplots(figsize=(20,20))
streets.plot(ax=ax, color = colors[4], linewidth= 0.2)
gdf.plot(ax=ax, color=colors[1], edgecolor=”white”, linewidth=0.3)
plt.title(‘Mälmo Neighbourhoods’, fontsize=40, fontname=”Palatino Linotype”, color=”grey”)
ax.axis(“off”)
#plt.axis(‘equal’)
plt.show()

Using the colours of your choice, you can enhance the visual aesthetics of your maps. In this example, we have modified the background colour of the map and gave Hex colour #678072 to the street lines.

Photo by Jen Theodore on Unsplash

The above map shows an effective use of colours and how to use colour choices with hex codes. To add context to our maps, we can add base maps.

Add Base maps

Adding background base maps was quite complicated earlier, but with the introduction of Contextily Library, we can easily different base maps to our maps. Let us do that.

fig, ax = plt.subplots(figsize=(16, 18))
gdf.to_crs(epsg=3857).plot(ax=ax, color=colors[1], edgecolor=”white”, linewidth=0.3, alpha=0.5) # 2 - Projected plot

streets.to_crs(epsg=3857).plot(ax=ax, color = colors[4], linewidth= 0.2)
ctx.add_basemap(ax, url=ctx.providers.Stamen.TonerLite) # 3
plt.title(‘Mälmo Neighbourhoods’, fontsize=40, fontname=”Palatino Linotype”, color=”grey”)
ax.axis(“off”)
#plt.axis(‘equal’)
plt.show()

We just projected the data to web Mercator before plotting it (# 2) and add a base map, in this case, Stamen Toner Lite design. And here is the map with base maps. Depending on your choice, you can choose many different base maps.

Photo by Jen Theodore on Unsplash

The map above has a contextual base map and is better visually compared to the defaults Geopandas provides. Do not accept defaults and try to experiment and design your maps. With some knowledge of Matplotlib, you can create an aesthetically pleasing map in Python.

Conclusion

In this tutorial, we covered how to go beyond defaults in map-making with Geopandas. We have seen how to tweak and customize plots with parameters. We have also shared how to include colour design in your maps. Finally, we have added base maps easily to provide context for the maps.

The code for this tutorial can be accessed in this Github repository.

Leave a Reply

Your email address will not be published. Required fields are marked *