Is there also a way to do make a lineplot with bandwidths in seaborn? Lineplot has a option to show the confidence interval (also see: Stackoverflow topic and documentation seaborn.lineplot). Or would someone advice me to do this with plotly/different package?
But what I'm looking for to plot a line with a signal and make several bandwiths arround it with a filled color (transparancy increases when bandwith is more away from the signal).
Based on this topic Fat band using matplotlib in python I've managed to do something in Matplotlib:
# Imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
# Create dataset
mean = np.random.randint(1,101,24)
max_val = np.random.randint(101,150,24)
min_val = np.random.randint(-50,1,24)
std = np.random.randint(10,30,24)
df = pd.DataFrame({'min': min_val,
'-3std': -3*std+mean,
'-2std': -2*std+mean,
'-1std': -1*std+mean,
'mean': mean,
'1std': 1*std+mean,
'2std': 2*std+mean,
'3std': 3*std+mean,
'max':max_val})
# function for plot
def plot_bandwidth(df, set_labels=True, colortone='blue', ax=None, show=False):
"""
Method to create a plot from a dataframe with the required columns [] and index
:param (pd.DataFrame) df: Dataframe with numeric values
:param (bool) set_labels: Boolean value to choose if labels are shown
:param (string) colortone: String with the color to use as base for different lines/areas
:param (ax) ax: Option to add axes to combine multiple plots
:param (bool) show: Boolean to show plot or return figure
:return plot/fig
"""
# TODO: Assert if required columns not available
if not ax:
_, ax = plt.subplots()
quarters_of_day = df.index
ax.plot(quarters_of_day, df['mean'], color=colortone)
ax.fill_between(quarters_of_day, df['-3std'], df['3std'], alpha=.1, color=colortone)
ax.fill_between(quarters_of_day, df['-2std'], df['2std'], alpha=.1, color=colortone)
ax.fill_between(quarters_of_day, df['-1std'], df['1std'], alpha=.1, color=colortone)
ax.plot(quarters_of_day, df['min'], color='dark'+colortone, ls='--', alpha=.4)
ax.plot(quarters_of_day, df['max'], color='dark'+colortone, ls='--', alpha=.4)
if set_labels == True:
ax.set_title("Example plot")
ax.set_xlabel("Hour")
ax.set_ylabel("Value")
legend_mean = mlines.Line2D([], [], color=colortone, label='Mean')
legend_bandwidth_std1 = mpatches.Patch(alpha=.3, color=colortone, label='Bandwidth of 1 sigma')
legend_bandwidth_std2 = mpatches.Patch(alpha=.2, color=colortone, label='Bandwidth of 2 sigma')
legend_bandwidth_std3 = mpatches.Patch(alpha=.1, color=colortone, label='Bandwidth of 3 sigma')
legend_minmax = mlines.Line2D([], [], color='dark'+colortone, ls='--', alpha=.4, label='Minimum or Maximum')
plt.legend(handles=[legend_mean, legend_bandwidth_std1, legend_bandwidth_std2, legend_bandwidth_std3, legend_minmax], loc='center left', bbox_to_anchor=(1, 0.5))
if show:
return plt.show()
plot_bandwidth(df)
This leads to a plot like this: