Duration Curve using Matplotlib

matplotlib_duration_plot

Skill - Duration Curve using Matplotlib

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


Matplotlib is a plotting library tn the scipy ecosystem of libraries.

Please make sure that you covered thepost on basics


Duration Curve helps us visualize the sample frequency distribution in a single plot.

Duration Plot values derivation function

import numpy as np
import pandas as pd

def deriveDurationVals(vals, valBinResol):
    samplVals = []
    percExceeded = []
    vals = pd.Series(vals)
    numVals = len(vals)
    min_value = vals.min()
    max_value = vals.max()

    for val in np.arange(min_value, max_value, valBinResol):
        samplVals.append(val)
        binExceededPerc = len(vals[vals > val])*100/numVals
        percExceeded.append(binExceededPerc)

    return {'sampl_vals': samplVals, 'perc_exceeded': percExceeded}

Example of duration curve

In the sample folder containing the example script, create a file named duration_plot.py and write the function shown above and use the function in the example as shown below

import matplotlib.pyplot as plt
from duration_plot import deriveDurationVals
# data samples
sampls = [50.061,50.055,50.043,50.050,...]

# derive duration plot values using the function 
durPltData = deriveDurationVals(sampls, 0.01)

# plot the duration curve
fig, ax = plt.subplots()
ax.plot(durPltData["perc_exceeded"], durPltData["sampl_vals"])
plt.show()

matplotlib_duration_plot_demo

Video

You can the video on this post here


Online Interpreter

Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://pynative.com/online-python-code-editor-to-execute-python-code/


Table of Contents

Comments