Skip to content

Commit

Permalink
load duration curve with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JrtPec committed Apr 26, 2018
1 parent baca349 commit 11df0c6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
24 changes: 24 additions & 0 deletions opengrid/library/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,27 @@ def carpet(timeseries, **kwargs):
plt.title(title)

return im


def load_duration_curve(df, trim_zeros=False, **kwargs):
"""
Plot a load duration curve
Parameters
----------
df : pd.DataFrame or pd.Series
trim_zeros : bool
trim trailing zero's
kwargs : anything you would pass to pd.DataFrame.plot()`
Returns
-------
` matplotlib plot
"""
df = pd.DataFrame(df) # in case a series is passed, wrap it in a dataframe
load_factors = (df[column].reset_index(drop=True).sort_values(ascending=False).reset_index(drop=True) for column in df)
if trim_zeros:
load_factors = (np.trim_zeros(s, trim='b') for s in load_factors)
df = pd.concat(load_factors, axis=1)
fig = df.plot(**kwargs)
return fig
8 changes: 8 additions & 0 deletions opengrid/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import unittest
import pandas as pd
import opengrid as og
from opengrid.library import plotting


Expand All @@ -26,5 +27,12 @@ def test_empty(self):
assert plotting.carpet(pd.Series(index=list('abc'))) is None


class LoadDurationCurveTest(unittest.TestCase):
def test_default(self):
df = og.datasets.get('gas_2016_hour')
assert og.plotting.load_duration_curve(df) is not None
assert og.plotting.load_duration_curve(df, trim_zeros=True) is not None


if __name__ == '__main__':
unittest.main()

0 comments on commit 11df0c6

Please sign in to comment.