Skip to content

Commit

Permalink
Merge 13d5061 into 51bd0ab
Browse files Browse the repository at this point in the history
  • Loading branch information
vindelico committed Mar 21, 2024
2 parents 51bd0ab + 13d5061 commit b4f4200
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .zenodo.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
"name": "Rondeau-Genesse, Gabriel",
"affiliation": "Ouranos",
"orcid": "0000-0003-3389-9406"
},
{
"name": "Braun, Marco",
"affiliation": "Ouranos",
"orcid": "0000-0001-5061-3217"
}
],
"keywords": [
Expand Down
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Contributors
------------

* Gabriel Rondeau-Genesse <rondeau-genesse.gabriel@ouranos.ca> `@RondeauG <https://github.com/RondeauG>`_
* Marco Braun <Braun.Marco@ouranos.ca> `@vindelico <https://github.com/vindelico>`_
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog

0.4.0 (unreleased)
------------------
Contributors to this version: Trevor James Smith (:user:`Zeitsperre`).
Contributors to this version: Trevor James Smith (:user:`Zeitsperre`), Marco Braun (:user:`vindelico`)

Internal changes
^^^^^^^^^^^^^^^^
Expand All @@ -19,6 +19,7 @@ Contributors to this version: Sarah-Claude Bourdeau-Goulet (:user:`Sarahclaude`)

New features and enhancements
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* Use list or ndarray as levels for colorbar in gridmap and small bug fixes (:pull:`???`).
* New function ``fg.matplotlib.hatchmap`` (:pull:`107`).
* Support for translating figures. Activating a locale through `xclim`'s ``metadata_locales`` option will try to use metadata saved by `xclim` or `xscen` in this locale and to translate common terms appearing in the figures. `figanos` currently ships with French translations of those terms. (:pull:`109`, :issue:`64`).
* New ``figanos.Logos`` class added to manage and install logos stored in user's Home configuration directory. The ``figanos.utils.plot_logo`` function call signature has changed to support the new system. (:issue:`115`, :pull:`119`).
Expand Down
45 changes: 25 additions & 20 deletions figanos/matplotlib/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import math
import warnings
from collections.abc import Iterable
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -525,7 +526,7 @@ def gridmap(
geometries_kw: dict[str, Any] | None = None,
contourf: bool = False,
cmap: str | matplotlib.colors.Colormap | None = None,
levels: int | None = None,
levels: int | list | np.ndarray | None = None,
divergent: bool | int | float = False,
show_time: bool | str | int | tuple[float, float] = False,
frame: bool = False,
Expand Down Expand Up @@ -563,8 +564,8 @@ def gridmap(
If None, look for common variables (from data/ipcc_colors/varaibles_groups.json) in the name of the DataArray
or its 'history' attribute and use corresponding colormap, aligned with the IPCC visual style guide 2022
(https://www.ipcc.ch/site/assets/uploads/2022/09/IPCC_AR6_WGI_VisualStyleGuide_2022.pdf).
levels : int, optional
Number of levels to divide the colormap into.
levels : int, list, np.ndarray, optional
Number of levels to divide the colormap into or list of level boundaries (in data units).
divergent : bool or int or float
If int or float, becomes center of cmap. Default center is 0.
show_time : bool, tuple, string or int.
Expand Down Expand Up @@ -682,15 +683,18 @@ def gridmap(
)
plot_kw.setdefault("cmap", cmap)

if levels:
lin = custom_cmap_norm(
cmap,
np.nanmin(plot_data.values),
np.nanmax(plot_data.values),
levels=levels,
divergent=divergent,
linspace_out=True,
)
if levels is not None:
if isinstance(levels, Iterable):
lin = levels
else:
lin = custom_cmap_norm(
cmap,
np.nanmin(plot_data.values),
np.nanmax(plot_data.values),
levels=levels,
divergent=divergent,
linspace_out=True,
)
plot_kw.setdefault("levels", lin)

elif (divergent is not False) and ("levels" not in plot_kw):
Expand All @@ -717,7 +721,7 @@ def gridmap(
# bug xlim / ylim + transfrom in facetgrids
# (see https://github.com/pydata/xarray/issues/8562#issuecomment-1865189766)
if transform and ("xlim" in plot_kw and "ylim" in plot_kw):
extend = [
extent = [
plot_kw["xlim"][0],
plot_kw["xlim"][1],
plot_kw["ylim"][0],
Expand All @@ -726,7 +730,7 @@ def gridmap(
plot_kw.pop("xlim")
plot_kw.pop("ylim")
elif transform and ("xlim" in plot_kw or "ylim" in plot_kw):
extend = None
extent = None
warnings.warn(
"Requires both xlim and ylim with 'transform'. Xlim or ylim was dropped"
)
Expand All @@ -735,7 +739,7 @@ def gridmap(
if "ylim" in plot_kw.keys():
plot_kw.pop("ylim")
else:
extend = None
extent = None

# plot
if ax:
Expand All @@ -749,8 +753,8 @@ def gridmap(
im = plot_data.plot.contourf(**plot_kw)

if ax:
if extend:
ax.set_extend(extend)
if extent:
ax.set_extent(extent)

ax = add_features_map(
data,
Expand Down Expand Up @@ -790,8 +794,8 @@ def gridmap(
geometries_kw,
frame,
)
if extend:
fax.set_extent(extend)
if extent:
fax.set_extent(extent)

# when im is an ax, it has a colorbar attribute. If it is a facetgrid, it has a cbar attribute.
if (frame is False) and (
Expand Down Expand Up @@ -1543,9 +1547,10 @@ def scattermap(
else:
raise ValueError("If `data` is a dict, it must be of length 1.")

# select data to plot
# select data to plot and its xr.Dataset
if isinstance(data, xr.DataArray):
plot_data = data
data = xr.Dataset({plot_data.name: plot_data})
elif isinstance(data, xr.Dataset):
if len(data.data_vars) > 1:
warnings.warn(
Expand Down
3 changes: 2 additions & 1 deletion figanos/matplotlib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pathlib
import re
import warnings
from copy import deepcopy
from tempfile import NamedTemporaryFile
from typing import Any, Callable

Expand Down Expand Up @@ -81,7 +82,7 @@ def empty_dict(param) -> dict:
"""Return empty dict if input is None."""
if param is None:
param = dict()
return param
return deepcopy(param) # avoid modifying original input dict when popping items


def check_timeindex(
Expand Down

0 comments on commit b4f4200

Please sign in to comment.