Skip to content

Commit 6cf2d9c

Browse files
committed
add test and docstring to the density method
1 parent 69aafa3 commit 6cf2d9c

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed
46.8 KB
Loading
77.1 KB
Loading

statista/time_series.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -777,15 +777,29 @@ def density(self, **kwargs) -> Tuple[Figure, Axes]:
777777
778778
Examples
779779
--------
780-
>>> ts = TimeSeries(np.random.randn(100))
781-
>>> fig, ax = ts.plot_density()
780+
- Plot the KDE density plot for a 1D time series:
781+
782+
>>> ts = TimeSeries(np.random.randn(100))
783+
>>> fig, ax = ts.density(title='Density Plot', xlabel='Random Values', ylabel='KDE density')
784+
785+
.. image:: /_images/time_series/density-1d.png
786+
:align: center
787+
788+
- Plot the KDE density plot for a 2D time series:
789+
790+
>>> ts = TimeSeries(np.random.randn(100, 4))
791+
>>> fig, ax = ts.density(title='Density Plot', xlabel='Random Values', ylabel='KDE density')
792+
793+
.. image:: /_images/time_series/density-2d.png
794+
:align: center
782795
"""
783796
fig, ax = self._get_ax_fig(**kwargs)
784797
color = kwargs.get("color", None)
785-
self[self.columns].plot(kind="density", ax=ax, color=color)
798+
self[self.columns.to_list()].plot(kind="density", ax=ax, color=color)
799+
kwargs.pop("ax", None)
786800
ax = self._adjust_axes_labels(
787801
ax,
788-
self.columns,
802+
kwargs.get("xtick_labels"),
789803
**kwargs,
790804
)
791805

tests/test_time_series.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,21 @@ def test_plot_rolling_statistics(self, ts: TimeSeries, request):
226226
fig2, ax2 = ts.rolling_statistics(fig=fig, ax=ax)
227227
assert fig2 is fig, "If fig is provided, plot_rolling_statistics should use it."
228228
assert ax2 is ax, "If ax is provided, plot_rolling_statistics should use it."
229+
230+
231+
class TestDensity:
232+
233+
@pytest.mark.parametrize("ts", ["ts_1d", "ts_2d"])
234+
def test_plot_density(self, ts: TimeSeries, request):
235+
"""Test the plot_density method."""
236+
ts = request.getfixturevalue(ts)
237+
fig, ax = ts.density()
238+
assert isinstance(
239+
fig, plt.Figure
240+
), "plot_density should return a matplotlib Figure."
241+
assert isinstance(ax, plt.Axes), "plot_density should return a matplotlib Axes."
242+
243+
fig, ax = plt.subplots()
244+
fig2, ax2 = ts.density(fig=fig, ax=ax)
245+
assert fig2 is fig, "If fig is provided, plot_density should use it."
246+
assert ax2 is ax, "If ax is provided, plot_density should use it."

0 commit comments

Comments
 (0)