Skip to content
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
27ac391
Style argument to timeseries plot
FrejaTerpPetersen Nov 5, 2024
c972683
first value is obs style
FrejaTerpPetersen Nov 5, 2024
3619190
Added color argument
FrejaTerpPetersen Nov 5, 2024
72d3210
Created function for checking inputs
FrejaTerpPetersen Nov 6, 2024
dfef139
Decided to only allow color OR style input
FrejaTerpPetersen Nov 6, 2024
a68fcfd
Wrote tests
FrejaTerpPetersen Nov 6, 2024
320563d
Deleted test call
FrejaTerpPetersen Nov 6, 2024
d55b4c7
docstring update
FrejaTerpPetersen Nov 6, 2024
d2d57e3
Design decision
FrejaTerpPetersen Nov 6, 2024
4446709
Removed unused import
FrejaTerpPetersen Nov 6, 2024
3a1fcba
Fix type error: color was not subscriptable if None
FrejaTerpPetersen Nov 6, 2024
0f57302
Wrong type error ... Moved default coloring
FrejaTerpPetersen Nov 6, 2024
1a70efb
Import default colors
FrejaTerpPetersen Nov 6, 2024
13a7163
Allow color to be tuple
FrejaTerpPetersen Nov 6, 2024
3f941aa
Moved import outside plot code
FrejaTerpPetersen Nov 6, 2024
f4d22ff
Merge remote-tracking branch 'origin/main' into timeseries-plot-style
jsmariegaard Nov 13, 2024
5cf2d03
Example notebook with timeseries plotting
jsmariegaard Nov 20, 2024
9337b8e
Specify only matplotlib backend in docstring
FrejaTerpPetersen Mar 12, 2025
fb44f8a
Moved check function from misc to top of comparer_plotter
FrejaTerpPetersen Mar 12, 2025
6b4476c
Rewrite check function
FrejaTerpPetersen Mar 12, 2025
5c0a029
remove unused code, update test
FrejaTerpPetersen Mar 12, 2025
9b69219
Not (yet) implemented in plotly
ecomodeller Mar 12, 2025
33f631a
Add plotly as test dependency
ecomodeller Mar 12, 2025
dc858ea
Merge branch 'main' into timeseries-plot-style
ecomodeller Mar 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions modelskill/comparison/_comparer_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
_xtick_directional,
_ytick_directional,
quantiles_xy,
_check_kwarg_and_convert_to_list,
)
from ..plotting import taylor_diagram, scatter, TaylorPoint
from ..settings import options
Expand Down Expand Up @@ -62,6 +63,8 @@ def timeseries(
ax=None,
figsize: Tuple[float, float] | None = None,
backend: str = "matplotlib",
style: list[str] | str | None = None,
color: list[str] | tuple | str | None = None,
**kwargs,
):
"""Timeseries plot showing compared data: observation vs modelled
Expand All @@ -79,32 +82,66 @@ def timeseries(
backend : str, optional
use "plotly" (interactive) or "matplotlib" backend,
by default "matplotlib"
style: list of str, optional
containing line styles of the model results. Cannot be passed together with color input.
by default None
color: list of str, optional
containing colors of the model results.
If len(colors) == num_models + 1, the first color will be used for the observations.
Cannot be passed together with style input.
by default None
**kwargs
other keyword arguments to fig.update_layout (plotly backend)

Returns
-------
matplotlib.axes.Axes or plotly.graph_objects.Figure
"""

from ._comparison import MOD_COLORS

cmp = self.comparer

if title is None:
title = cmp.name

if color is not None and style is not None:
raise ValueError(
"It is not possible to pass both the color argument and the style argument. Choose one."
)

# Color for observations:
obs_color = cmp.data[cmp._obs_name].attrs["color"]

# if color is None and style is None: # Use default values for colors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unused code

# from ._comparison import MOD_COLORS

# color = MOD_COLORS[: cmp.n_models]

color, style = _check_kwarg_and_convert_to_list(color, style, cmp.n_models)

if color is not None and len(color) > cmp.n_models:
# If more than n_models colors is given, the first color is used for the observations
obs_color = color[0]
color = color[1:]

if backend == "matplotlib":
fig, ax = _get_fig_ax(ax, figsize)
for j in range(cmp.n_models):
key = cmp.mod_names[j]
mod = cmp.raw_mod_data[key]._values_as_series
mod.plot(ax=ax, color=MOD_COLORS[j])
if style is not None:
mod.plot(ax=ax, style=style[j])
else:
if color is None:
color = MOD_COLORS
mod.plot(ax=ax, color=color[j])

ax.scatter(
cmp.time,
cmp.data[cmp._obs_name].values,
marker=".",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style does not affect the observations, is this the intended behaviour?

Copy link
Author

@FrejaTerpPetersen FrejaTerpPetersen Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, observations can't have their style changed now. THis is a design choice, I guess? We could allow the user to input style for obs? Might lead to confusion, however, since I guess the user is pretty happy with a dot for the obs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the color can be changed. But not the marker.

color=cmp.data[cmp._obs_name].attrs["color"],
color=obs_color,
)
ax.set_ylabel(cmp._unit_text)
ax.legend([*cmp.mod_names, cmp._obs_name])
Expand Down
23 changes: 23 additions & 0 deletions modelskill/plotting/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,26 @@ def _format_skill_line(
name = name.upper()

return f"{name}", " = ", f"{fvalue} {item_unit}"


def _check_kwarg_and_convert_to_list(color, style, n_mod):
if isinstance(style, str):
# If style is str, convert to list (for looping)
style = [style]
if isinstance(color, str):
# Same with color
color = [color]

if color is not None and len(color) < n_mod: # too few colors given?
raise ValueError(
"Number of colors in 'color' argument does not match the number of models in the comparer."
)

if (
style is not None and len(style) < n_mod and style[0] is not None
): # too few styles given?
raise ValueError(
"Number of styles in 'style' argument does not match the number of models in the comparer."
)

return color, style
21 changes: 21 additions & 0 deletions tests/test_comparer.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,3 +887,24 @@ def test_from_matched_dfs0():
assert float(
gs.data.sel(x=-0.01, y=55.1, method="nearest").rmse.values
) == pytest.approx(0.0476569069177831)


def test_timeseriesplot_accepts_style_color_input(pc):
# Check that it can take the inputs
ax = pc.plot.timeseries(color=["red", "blue"])
ax = pc.plot.timeseries(style=["b-", "g--"])
assert ax.lines[1].get_color() == "g"

# Check that errors are raised
with pytest.raises(ValueError, match="Choose one"):
ax = pc.plot.timeseries(color=["red", "blue"], style="b-")
with pytest.raises(ValueError, match="'color' argument"):
ax = pc.plot.timeseries(color=["red"])
with pytest.raises(ValueError, match="'style' argument"):
ax = pc.plot.timeseries(style=["b-"])

ax = pc.plot.timeseries(color=["red", "blue", "black"])
# first line is blue (red is for observations).
assert ax.lines[0].get_color() == "blue"
plt.show()
print("Hello")
Loading