Skip to content

add warnings for consistent legend sizes in combined geographical plots#1178

Merged
fneum merged 3 commits intomasterfrom
fneum/legend-circle-sizes
Mar 19, 2025
Merged

add warnings for consistent legend sizes in combined geographical plots#1178
fneum merged 3 commits intomasterfrom
fneum/legend-circle-sizes

Conversation

@fneum
Copy link
Copy Markdown
Member

@fneum fneum commented Mar 18, 2025

Changes proposed in this Pull Request

There are some interactions with combined PyPSA and Geopandas plots on a single geographical axis. Depending on the order of execution, the legend circle sizes are correct or incorrext (add_legend_circles() and add_legend_semicircles()).

The problem is the changing extent of the plot, which is changed here:

https://github.com/PyPSA/pypsa/blob/fneum/legend-circle-sizes/pypsa/plot.py#L242-L243

and accessed here to calculate in projected_area_factor().

https://github.com/PyPSA/pypsa/blob/df3beba8b8aec071fcbe61800d6358552d76eba6/pypsa/plot.py#L534

I have no idea how to properly it (apart from running n.plot() first), but a first measure would be to warn about what the right order of execution would be.

MWE

import pypsa
import matplotlib.pyplot as plt
from pypsa.plot import add_legend_circles, add_legend_semicircles
import cartopy.crs as ccrs
import geopandas as gpd

n = pypsa.examples.ac_dc_meshed()
gdf = gpd.read_file(fn)

# a) works as intended

crs = ccrs.PlateCarree()
fig, ax = plt.subplots(figsize=(8, 8), subplot_kw={"projection": crs})
n.plot(ax=ax, bus_sizes=2, margin=0.8)
add_legend_circles(ax, 2, "", legend_kw=dict(frameon=False, loc="center"))
add_legend_semicircles(ax, 2, "", legend_kw=dict(frameon=False, loc="lower center"))
gdf.to_crs(crs.proj4_init).plot(ax=ax, color="none")

# b) wrong marker size

crs = ccrs.PlateCarree()
fig, ax = plt.subplots(figsize=(8, 8), subplot_kw={"projection": crs})
gdf.to_crs(crs.proj4_init).plot(ax=ax, color="none")
n.plot(ax=ax, bus_sizes=2, margin=0.8)
add_legend_circles(ax, 2, "", legend_kw=dict(frameon=False, loc="center"))
add_legend_semicircles(ax, 2, "", legend_kw=dict(frameon=False, loc="lower center"))

Output of a)

image

Output of b)

image

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

@fneum fneum requested a review from lkstrp March 18, 2025 16:04
@lkstrp lkstrp enabled auto-merge (squash) March 19, 2025 09:13
@fneum fneum disabled auto-merge March 19, 2025 17:42
@fneum fneum merged commit c5babed into master Mar 19, 2025
4 of 5 checks passed
@fneum fneum deleted the fneum/legend-circle-sizes branch March 19, 2025 17:42
lkstrp added a commit that referenced this pull request Mar 23, 2025
FabianHofmann added a commit that referenced this pull request Mar 24, 2025
* fix: `cartopy` warning

* refactor: clean up and move to `plot.py`

* refactor: start bigger `plot.py` refactoring

* feat: use deprecation warning

* feat: add deprecate kwargs decorator and deprecate cmap norms

* refactor: modularise branch plotting

* refactor: more clean up

* refactor: add `NetworkPlotter` class

* feat: close #302

* feat: return collections as dict

* fix: don't filter deprecation warnings

* refactor: deprecate `color_geomap` to `geomap_colors`

* docs: add types and update docstring

* follow up

* plot: refactor

* plot: refactor data series alignment for widhts, colors, alpha
add legend for arrows

* add balance plot function

* plot: write static plots as functions on self

* plot: fix axis and boundaries assignment

* plot balance: add patch legend

* plot: make new and old line scaling switchable

* plot: ensure correct digit scaling in auto-unit

* plot: fix flow aggregation & flow legend scaling

* plot: track bus carrier units

* follow up

* fix: adress review comments

* init restructure

* follow up

* add tests

* add plotting consistency check to plotting

* accessor: add carrier and component subplot handling

* add more tests

* test: Add comprehensive tests for plot accessors module

* test: Add comprehensive test coverage for plot accessors module

* fix: update test data to use valid carriers and avoid melt conflict

* fix: ensure default gray colors are always included in carrier colors

* fix: ensure default gray colors are always included in carrier colors

* Simplify carrier color mapping logic in `_get_carrier_colors`

* Fix `_create_base_plot` to handle missing carrier column

* refactor: generalize plotting routines with dynamic column handling

* Fix deprecation warnings and improve categorical data handling in plotting

* refactoring

* plot: fix negative and positive stacking

* plot: follow up

* plot_accessor: adjust tests

* feat: add carrier attribute to statistics functions

* feat: harmonise statistics maps plotter

* sanitize ac_dc_meshed carriers and colors
add statistics map plots

* fix: capacity attribute in expressions, docstrings

* feat: deprecated old pypsa.plot namespace

* feat: complete type annotation in maps.py

* fix: remove unneeded type ignores

* fix: reinsert sanitize ac_dc_meshed, fix import from netcdf

* rename utils.py to common.py to ease merge

* fix: don't reuse self.ax per default in maps plots

* feat: add carrier selection to statistics
      use explicit transformed points in the plotting routine

* Plotting Library - Part 2

* review

* fix tests

* Update pypsa/plot/accessor.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: time aggregation in statistic plots

* draft schema

* create dedicated module for statistics maps

* fix tests/ review

* rename col/row to facet_col/facet_row

* fix doc test

* improve handling of stat_kwargs

* fix: ingore ax type in statistic maps

* more linter rules and docstrings

* fix type checker

* fix pre-commit

* ifx tests

* feat: use statistics handler as drop in method replacement

* release note

* fix type checker

* improved release notes

* move `n.plot.energy_balance` to `n.statistics.energy_balance`

* deprecate old namespace

* fix tests

* fix types

* add `.plot` accessor

* feat: use seaborn facet grid instead of seaborn objects

* rename test_plot_charts

* save test plots option

* improved schema and add `kind` argument for `.plot()`

* fix base plot test

* add plot hashs in tests

* fix: nice names for both charts and maps
fix: area and bar plots for facet grid
feat: add arguments for sharey and sharex

* fix: imports in test_plot_stats

* fix: refine case distinction for custom chart plot routine

* fix doc tests

* fix: types and docstrings in plotter.py

* refac: facilitate charts.py

* feat: improve defaults

* doctests

* improved plot hash tests

* hash tests

* refac: define arguments in chart functions
fix: weid pandas ylim behavior

* fix: default arguments for static area plots

* feat: backwardscompatible flow argument, pull in #1179

* update plot hashes

* support missing colors in statistic map plot

* install cartopy in ci

* fix: ignore types for _shared_axes

* update plot hashes

* use `pytest-mpl`

* take over #1178

* refac remove unneeded cartopy tests

* fix tests

* use fix fig size

* add seed

* fix: linopy deprecation in examples

* use fixed dpi

* also use fixed figsize

* fix: try to reorder multiplication in doc example

* another dpi

* use global rc setting

* fix: multiplication for numpy version >= 2.0

* change figsize again

* reset to rc defaults

* disable mpl test on maps

* fix: deprecation case for flow argument

---------

Co-authored-by: Fabian <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

2 participants