python - 在seaborn(python)中绘制具有多个带宽的线
问题描述
还有一种方法可以在 seaborn 中制作具有带宽的线图吗?Lineplot 可以选择显示置信区间(另请参阅:Stackoverflow 主题和文档seaborn.lineplot)。或者有人会建议我用情节/不同的包装来做到这一点吗?
但是我正在寻找用信号绘制一条线并用填充颜色围绕它的几个带宽(当带宽更远离信号时透明度会增加)。
基于这个主题Fat band using matplotlib in python我已经设法在 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)
这导致了这样的情节:
解决方案
推荐阅读
- python - 使用混合类型将列转换为日期。“to_datetime”不起作用
- java - 读取空格分隔的整数Java
- corda - 如何定制 Corda 医院?
- dart - Dart 中的神秘匿名函数
- python - 如何在 Python 3 中搜索并获取具有两个搜索词的字符串中的第二个词?
- c# - .NET Core API 抛出 Autofac.Core.DependencyResolutionException
- python - 数据集的学习曲线
- java - 如何使用 textwatcher 获取警察号码
- javascript - 如何在Javascript中制作平方数
- javascript - 每当我在反应应用程序中添加过滤器功能时面临的问题