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.

BUG: Incorrect colours when plotting multiple geometry types with list of colours

See original GitHub issue

Expected result

I have a geopandas.GeoDataFrame that contains multiple geometry types (e.g. Polygon, LineString and Point). I want to plot this data by supplying a list of colours (one per feature) to the color= parameter:

from shapely.geometry import LineString, Point, Polygon
import geopandas as gpd

gdf = gpd.GeoDataFrame(geometry=[Polygon([(1, 1), (1, 2), (2, 2), (2, 1)]),
                                LineString([(2, 0), (4, 2)]),
                                Point(3, 2),
                                Point(1, 0)])

gdf.plot(color=['red', 'green', 'blue', 'blue'])

For this example, I would expect that:

  1. My Polygon feature will be plotted in “red”
  2. My LineString feature will be plotted in “green”
  3. Both of my Point features will be plotted in “blue”

Actual result

However, I receive this result:

image

It appears to me like the list of colours is being applied separately to each geometry type, with the first item of each type being plotted in “red”, and the second item of each type being plotted in “green” (e.g. see the Point features).

This appears related to the PR and linked issues here (#1119), but those look like they refer specifically to multi-geometries, and not multiple geometry types.

Environment geopandas '0.7.0' Python 3.6.9 (default, Nov 7 2019, 10:44:02) IPython 7.12.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
martinfleiscommented, Apr 16, 2020

Thank you for the report!

Your assumption is correct. Each geometry type is now plotted as an independent Collection, to which we pass color as it is (assuming it is a single value). For list-like color values of the correct length, we should do the same we do with value, i.e. decomposing the list based on the geom type.

0reactions
martinfleiscommented, Apr 22, 2022

could be considered an misunderstanding of color parameter. It’s the sequence in which colors are used based on column parameter.

Not necessarily. You can plot geometries without using a column and then the idea is that you are able to pass a list of colours that is mapped to individual geometries. It is just broken when you have mixed geom types.

if length of color parameter is same as dataframe, create an internal column and a LinearSegmentedColormap() as per examples above

That may work, yes. You don’t even need a column as column can take an array.

But the solution I outlined above would also work:

For list-like color values of the correct length, we should do the same we do with value, i.e. decomposing the list based on the geom type.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Geopandas not plotting correct colors - python - Stack Overflow
1. Don't Post Screenshots. · 2. This is a known bug in GeoPandas when having mixed geometry types and passing the colors like...
Read more >
Observing wrong colours in ggplot line plots using two ...
I have two dataframes, each having one hundred 'x' and 'y' coordinates I wish to plot together on a single line plot.
Read more >
Multiple color (and fill) scales with ggplot2 - Elio Campitelli
In the Atmospheric Sciences, for example, plotting temperature and pressure in contour lines with different color scales is a common practice.
Read more >
Drawing objects do not plot and are missing from print preview ...
When plotting from AutoCAD, one or more drawing elements or lines do ... Incorrect Plotter Configuration. ... Incorrect Layer Color type.
Read more >
Four color theorem - Wikipedia
In mathematics, the four color theorem, or the four color map theorem, states that no more than four colors are required to color...
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