Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent and concise datetime axis on timeseries plots (matplotlib) #347

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion modelskill/comparison/_comparer_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .. import metrics as mtr
from ..utils import _get_idx
import matplotlib.colors as colors
import matplotlib.dates as mdates
from ..plotting._misc import (
_get_fig_ax,
_xtick_directional,
Expand Down Expand Up @@ -96,7 +97,8 @@ def timeseries(
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])
# mod.plot(ax=ax, color=MOD_COLORS[j])
ax.plot(mod.index, mod.values, color=MOD_COLORS[j])

ax.scatter(
cmp.time,
Expand All @@ -110,6 +112,12 @@ def timeseries(
if self.is_directional:
_ytick_directional(ax, ylim)
ax.set_title(title)

# concise datetime labels
locator = mdates.AutoDateLocator(minticks=3, maxticks=12)
formatter = mdates.ConciseDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
return ax

elif backend == "plotly": # pragma: no cover
Expand Down
46 changes: 26 additions & 20 deletions modelskill/timeseries/_plotter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Protocol
import matplotlib.pyplot as plt
import matplotlib.dates as mdates


class TimeSeriesPlotter(Protocol):
Expand All @@ -19,17 +21,17 @@ class MatplotlibTimeSeriesPlotter(TimeSeriesPlotter):
def __init__(self, ts) -> None:
self._ts = ts

def __call__(self, **kwargs):
# default to timeseries plot
self.timeseries(**kwargs)

def __call__(self, title=None, color=None, marker=".", linestyle="None", **kwargs):
self.timeseries(
title=title, color=color, marker=marker, linestyle=linestyle, **kwargs
)

def timeseries(
self, title=None, color=None, marker=".", linestyle="None", **kwargs
):
"""Plot timeseries

Wraps pandas.DataFrame plot() method.

Parameters
----------
title : str, optional
Expand All @@ -40,18 +42,27 @@ def timeseries(
plot marker, by default '.'
linestyle : str, optional
line style, by default None
**kwargs
other keyword arguments to df.plot()
kwargs: other keyword arguments to df.plot()
"""
kwargs["color"] = self._ts._color if color is None else color
ax = self._ts._values_as_series.plot(
marker=marker, linestyle=linestyle, **kwargs
)

title = self._ts.name if title is None else title
ax.set_title(title)
t = self._ts._values_as_series.index
y = self._ts._values_as_series.values
plt.plot(
t,
y,
self._ts._values_as_series,
marker=marker,
linestyle=linestyle,
color=self._ts.color if color is None else color,

ax.set_ylabel(str(self._ts.quantity))
)
ax = plt.gca()
locator = mdates.AutoDateLocator(minticks=3, maxticks=12)
formatter = mdates.ConciseDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)

if title:
ax.set_title(title)
return ax

def hist(self, bins=100, title=None, color=None, **kwargs):
Expand Down Expand Up @@ -89,14 +100,9 @@ def __init__(self, ts) -> None:
self._ts = ts

def __call__(self):
# default to timeseries plot
self.timeseries()

def timeseries(self):
"""Plot timeseries

Wraps plotly.express.line() function.
"""
import plotly.express as px # type: ignore

fig = px.line(
Expand Down
1,891 changes: 1,881 additions & 10 deletions notebooks/Simple_timeseries_compare.ipynb

Large diffs are not rendered by default.

Loading