'Geopandas not plotting correct colors

My Geopandas DataFrame has 3 polygons and 9 points with color_rgba column computed with matplotlib.colors.to_rgba function:

import contextily as ctx
import geopandas as gpd

(...)
rdf[['geometry','color_rgba']]

enter image description here

When I try to plot the map something odd happens with 3 of the 9 points:

  • They inherit the color (dark) of the polygons and I can't understand how.
  • They should be all white as per the color_rgba column.

Code:

ax = rdf.plot(figsize=(20, 20), markersize = 100, edgecolor='k', color=rdf['color_rgba'])
xlim = ([-9.30, -9.00])
ylim = ([38.60, 38.87])
ax.set_xlim(xlim)
ax.set_ylim(ylim)
ctx.add_basemap(ax, crs=df.crs, url=ctx.providers.Stamen.TonerLite)
ax.set_axis_off()

enter image description here



Solution 1:[1]

  • as noted by @joris there is a known bug. I intend to work on a PR to fix so wanted this as a test case as well
  • have created geometry similar to that you have shown as screen shot in question
  • in interim, the following can be used as a work around
cmap = LinearSegmentedColormap.from_list(
    "seg", rdf["color_rgba"].unique(), N=len(rdf["color_rgba"].unique())
)
ax = rdf.plot(
    figsize=(20, 20), markersize=100, edgecolor="k", column="color_rgba", cmap=cmap
)

full code

import geopandas as gpd
import contextily as ctx
import matplotlib.pyplot as plt
import shapely.wkt, shapely.geometry
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

# reconstruct some geometry as not provided in question
lisbon = "POLYGON ((-9.2298356 38.69532759999999, -9.2159024 38.6913994, -9.1252283 38.70985619999998, -9.0932228 38.74978019999999, -9.0863328 38.79622539999999, -9.0993972 38.79637019999999, -9.0991256 38.7752176, -9.1450919 38.788146499999996, -9.1498877 38.7958538, -9.1831151 38.7785452, -9.2090617 38.7549077, -9.2079549 38.7241641, -9.2298356 38.69532759999999))"
b = np.array(shapely.wkt.loads(lisbon).bounds)
rdf = gpd.GeoDataFrame(
    {
        "geometry": [
            shapely.wkt.loads(lisbon).buffer(0.02, cap_style=2, join_style=2),
            shapely.wkt.loads(lisbon),
            shapely.wkt.loads(lisbon).buffer(-0.02, cap_style=2, join_style=2),
        ]
        + [
            shapely.geometry.Point(xy)
            for xy in zip(np.linspace(*b[[0, 2]], 9), np.linspace(*b[[1, 3]], 9))
        ],
        "color_rgba": [(0.0, 0.0, 0.0, 0.5), (0.0, 0.0, 0.0, 0.2), (0.0, 0.0, 0.0, 0.1)]
        + [(1.0, 1.0, 1.0, 1.0) for _ in range(9)],
    },
    crs="epsg:4386",
)

# ax = rdf.plot(figsize=(20, 20), markersize = 100, edgecolor='k', color=rdf['color_rgba'])
cmap = LinearSegmentedColormap.from_list(
    "seg", rdf["color_rgba"].unique(), N=len(rdf["color_rgba"].unique())
)
ax = rdf.plot(
    figsize=(20, 20), markersize=100, edgecolor="k", column="color_rgba", cmap=cmap
)

xlim = [-9.30, -9.00]
ylim = [38.60, 38.87]
ax.set_xlim(xlim)
ax.set_ylim(ylim)
ctx.add_basemap(ax, crs=rdf.crs, url=ctx.providers.Stamen.TonerLite)
ax.set_axis_off()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rob Raymond