question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Geopandas Plotting & Legends Symbology - QUESTION

See original GitHub issue

Hi All. I have a question about how plots and legends work with geopandas. I set the colors and line symbology using a list. And i used .plot() to plot the data

Here is a plot that i made of roads using the cropped natural earth data:

# plotting using geopandas
road_colors = ['black', 'grey', 'grey', 'black', 'grey', 'grey' ]
line_widths = [1, .5, .5, 1, .5,.5]
# plot the data
fig, ax = plt.subplots(figsize  = (12, 8))
country_boundary_us.plot(alpha = 1, color="white", 
                         edgecolor = "black", 
                         ax = ax)
us_roads_only.plot(ax=ax, column='type', 
                   cmap = ListedColormap(road_colors), 
                   linewidth = line_widths,
                   legend = True)
ax.set_axis_off()
plt.axis('equal');

image

Notice that my legend shows the road symbols as circles. Yet i’d prefer lines.

Here – i try to create a nicer legend using ax.legend().

# make it a bit nicer using a dictionary to assign colors and line widths
road_attrs = {'Beltway': ['black',2], 
               'Secondary Highway': ['grey',.5],
               'Road': ['grey',.5],
               'Bypass': ['grey', .5], 
               'Ferry Route': ['grey', .5], 
               'Major Highway': ['black', 1]}

# plot the data
fig, ax = plt.subplots(figsize  = (12, 8))

for ctype, data in us_roads_only.groupby('type'):
    data.plot(color=road_attrs[ctype][0], 
              label = ctype,
              ax = ax, 
              linewidth=road_attrs[ctype][1])

country_boundary_us.plot(alpha = 1, color="white", edgecolor = "black", ax = ax)
ax.legend(title="legend")
ax.set_title("United States Roads by Type", fontsize=25)
ax.set_axis_off()
plt.axis('equal');

image

I may be missing something. But can geopandas create a legend using the unique symbology applied to each (group) like ax.legend() can do natively if there is a label object? Or can you kindly help me understand how i would customize a legend created by geopandas? Perhaps my approach is wrong.

My GOAL: i’m trying to figure out the best way to teach students how to create a map with a legend in python.

Many thanks for any guidance.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lwassercommented, Nov 19, 2020

HAHAHA got it @martinfleis 😃 with COVID and the cold evenings on the horizon perhaps i will find some time to have a look at plotting.py 😃 it’s been on my mind for a while. Thank you!

1reaction
lwassercommented, Jul 16, 2018

thanks @jdmcbr ok that makes sense. I had a look at the code and suspected that was the case but i wasn’t fully certain. it would be a very nice feature to have. The matplotlib legend functionality is actually quite nice so it does seem as if geopandas could take advantage of that potentially. I appreciate your response!

Read more comments on GitHub >

github_iconTop Results From Across the Web

geopandas plotting with legend filled up with slash lines
When you create polygons, the property facecolor defines the fill color. And to create correct legend for polygon features, mpatches.
Read more >
Customize Map Legends and Colors in Python using Matplotlib
In this lesson, you will learn how to customize map symbology or the colors and symbols used to represent vector data in Python....
Read more >
Newest 'legend' Questions - GIS Stack Exchange
I am trying to programmatically set vector legend Text on Symbols with PyQGIS. This can be manually performed from Layer Properties -> Legend...
Read more >
Mapping and Plotting Tools - GeoPandas
geopandas makes it easy to create Choropleth maps (maps where the color of each shape is based on the value of an associated...
Read more >
Choropleth Mapping — Geographic Data Science with Python
The word choropleth stems from the root “choro”, meaning “region”. ... pandas import geopandas import pysal import numpy import matplotlib.pyplot as plt ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found