From d74caee1304ddc1fbb1031f920cb52b1dc06fba6 Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 5 Nov 2018 11:29:22 -0800 Subject: [PATCH 1/4] datetime64 slider text support --- animatplot/animation.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/animatplot/animation.py b/animatplot/animation.py index 24f847a..08c6bce 100644 --- a/animatplot/animation.py +++ b/animatplot/animation.py @@ -1,6 +1,8 @@ from matplotlib.animation import FuncAnimation, PillowWriter from matplotlib.widgets import Button, Slider import matplotlib.pyplot as plt +import numpy as np + from animatplot import Timeline @@ -132,8 +134,11 @@ def timeline_slider(self, text='Time', ax=None, valfmt='%1.2f', color=None): def set_time(t): self.timeline.index = int(self.slider.val) - self.slider.valtext.set_text( - self.slider.valfmt % (self.timeline[self.timeline.index])) + if np.issubdtype(self.timeline.t.dtype, np.datetime64): + self.slider.valtext.set_text((self.timeline[self.timeline.index])) + else: + self.slider.valtext.set_text( + self.slider.valfmt % (self.timeline[self.timeline.index])) if self._pause: for block in self.blocks: block._update(self.timeline.index) From 9d3ee51d331ab95c3d826645c522ceca496d9b8a Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 4 Feb 2019 09:29:00 -0700 Subject: [PATCH 2/4] Updates --- animatplot/animation.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/animatplot/animation.py b/animatplot/animation.py index 08c6bce..a6522e1 100644 --- a/animatplot/animation.py +++ b/animatplot/animation.py @@ -97,7 +97,7 @@ def pause(event): self._pause ^= True self.button.on_clicked(pause) - def timeline_slider(self, text='Time', ax=None, valfmt='%1.2f', color=None): + def timeline_slider(self, text='Time', ax=None, valfmt=None, color=None): """Creates a timeline slider. Parameters @@ -121,6 +121,12 @@ def timeline_slider(self, text='Time', ax=None, valfmt='%1.2f', color=None): else: self.slider_ax = ax + if valfmt is None: + if np.issubdtype(self.timeline.t.dtype, np.datetime64): + valfmt = '%s' + else: + valfmt = '%1.2f' + if self.timeline.log: valfmt = '$10^{%s}$' % valfmt @@ -134,11 +140,8 @@ def timeline_slider(self, text='Time', ax=None, valfmt='%1.2f', color=None): def set_time(t): self.timeline.index = int(self.slider.val) - if np.issubdtype(self.timeline.t.dtype, np.datetime64): - self.slider.valtext.set_text((self.timeline[self.timeline.index])) - else: - self.slider.valtext.set_text( - self.slider.valfmt % (self.timeline[self.timeline.index])) + self.slider.valtext.set_text( + self.slider.valfmt % (self.timeline[self.timeline.index])) if self._pause: for block in self.blocks: block._update(self.timeline.index) From 08b6e5fc9c1d940dcecd45a8e7fbd84bdb451e7a Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 4 Feb 2019 16:00:18 -0700 Subject: [PATCH 3/4] Update docstring and add support for timedelta64 --- animatplot/animation.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/animatplot/animation.py b/animatplot/animation.py index a6522e1..564c170 100644 --- a/animatplot/animation.py +++ b/animatplot/animation.py @@ -108,7 +108,7 @@ def timeline_slider(self, text='Time', ax=None, valfmt=None, color=None): The matplotlib axes to attach the slider to. valfmt : str, optional a format specifier used to print the time - Defaults to '%1.2f' + Defaults to '%s' for datetime64, timedelta64 and '%1.2f' otherwise. color : The color of the slider. """ @@ -122,7 +122,8 @@ def timeline_slider(self, text='Time', ax=None, valfmt=None, color=None): self.slider_ax = ax if valfmt is None: - if np.issubdtype(self.timeline.t.dtype, np.datetime64): + if (np.issubdtype(self.timeline.t.dtype, np.datetime64) + or np.issubdtype(self.timeline.t.dtype, np.timedelta64)): valfmt = '%s' else: valfmt = '%1.2f' From b7723cd2c679935b38b81de358ad8461e74a8884 Mon Sep 17 00:00:00 2001 From: dcherian Date: Mon, 4 Feb 2019 16:00:57 -0700 Subject: [PATCH 4/4] lint --- animatplot/animation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/animatplot/animation.py b/animatplot/animation.py index 564c170..ce19522 100644 --- a/animatplot/animation.py +++ b/animatplot/animation.py @@ -123,7 +123,7 @@ def timeline_slider(self, text='Time', ax=None, valfmt=None, color=None): if valfmt is None: if (np.issubdtype(self.timeline.t.dtype, np.datetime64) - or np.issubdtype(self.timeline.t.dtype, np.timedelta64)): + or np.issubdtype(self.timeline.t.dtype, np.timedelta64)): valfmt = '%s' else: valfmt = '%1.2f'