Skip to content

Fix kwargs in plot_graph#122

Merged
yu-ta-sato merged 1 commit intoc2g-dev:mainfrom
yu-ta-sato:fix/fix_kwargs_in_plot_graph
Feb 26, 2026
Merged

Fix kwargs in plot_graph#122
yu-ta-sato merged 1 commit intoc2g-dev:mainfrom
yu-ta-sato:fix/fix_kwargs_in_plot_graph

Conversation

@yu-ta-sato
Copy link
Copy Markdown
Collaborator

Description

Fixed a plotting bug in plot_graph for homogeneous graphs when legend=True, legend_kwargs, and title are provided

Problem

plot_graph was forwarding legend, legend_kwargs, and title directly into lower-level GeoDataFrame.plot() kwargs. In this path:

  • legend_kwargs leaked into edge plotting and triggered:
    • IndexError: index 1 is out of bounds for axis 0 with size 1
  • title was also incorrectly passed through instead of being applied on the axes.

Root Cause

In _plot_homo_graph and style resolution, plot-control kwargs (legend, legend_kwargs, title) were not properly treated as city2graph-managed parameters. They were unintentionally propagated to GeoPandas/Matplotlib collection plotting calls.

Changes

  • Marked legend, legend_kwargs, and title as reserved c2g keys so they are excluded from generic style kwargs.
  • Updated homogeneous plotting flow to:
    • strip title, legend, legend_kwargs before edge/node plotting,
    • apply legend settings only on node plotting,
    • map legend_kwargs to GeoPandas’ expected legend_kwds,
    • set title via ax.set_title(...) after plotting.
  • Improved plot parameter handling for column-driven styling by resolving pd.Series/column refs to numpy arrays where needed for consistent plotting behavior.

Validation

  • Reproduced the originally reported failing snippet and confirmed it now runs successfully.
### Failed before

import geopandas as gpd
from shapely.geometry import Point, LineString
import matplotlib.pyplot as plt
import city2graph as c2g

nodes = gpd.GeoDataFrame(
    {"centrality_quantile": [1, 2]},
    geometry=[Point(0, 0), Point(1, 1)],
    crs="EPSG:4326",
)

edges = gpd.GeoDataFrame(
    {"frequency": [100, 200]},
    geometry=[LineString([(0, 0), (1, 1)]), LineString([(1, 1), (2, 2)])],
    crs="EPSG:4326",
)

fig, ax = plt.subplots(figsize=(5, 5))

c2g.plot_graph(
    nodes=nodes,
    edges=edges,
    node_color="centrality_quantile",
    edge_color="#bbbbbb",
    edge_linewidth=edges["frequency"] / 200,
    legend=True,
    legend_kwargs={"label": "Betweenness Centrality", "orientation": "horizontal"},
    title = "Central London Transit Network Betweenness Centrality of Stops",
    ax=ax,
)

plt.show()
test_bug_with_legend
### Works
# Removing `legend=True`, `legend_kwargs`, and `title`  a makes the same call succeed.

import geopandas as gpd
from shapely.geometry import Point, LineString
import matplotlib.pyplot as plt
import city2graph as c2g

nodes = gpd.GeoDataFrame(
    {"centrality_quantile": [1, 2]},
    geometry=[Point(0, 0), Point(1, 1)],
    crs="EPSG:4326",
)

edges = gpd.GeoDataFrame(
    {"frequency": [100, 200]},
    geometry=[LineString([(0, 0), (1, 1)]), LineString([(1, 1), (2, 2)])],
    crs="EPSG:4326",
)

fig, ax = plt.subplots(figsize=(5, 5))

c2g.plot_graph(
    nodes=nodes,
    edges=edges,
    node_color="centrality_quantile",
    edge_color="#bbbbbb",
    edge_linewidth=edges["frequency"] / 200,
    # legend=True,
    # legend_kwargs={"label": "Betweenness Centrality", "orientation": "horizontal"},
    # title = "Central London Transit Network Betweenness Centrality of Stops"
    ax=ax,
)

plt.show()
test_bug_without_legend
  • Added a regression test covering the homogeneous case with node_color column, edge_linewidth series, legend=True, legend_kwargs, and title.

Related Issues

#121

Checklist

  • I have read the Contributing Guide.
  • I have updated the documentation, if necessary.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • Pre-commit checks passed locally.

@yu-ta-sato yu-ta-sato merged commit 22f8f03 into c2g-dev:main Feb 26, 2026
6 checks passed
@yu-ta-sato yu-ta-sato deleted the fix/fix_kwargs_in_plot_graph branch February 26, 2026 22:30
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant