Python 如何将seaborn BoxPlot按顺序添加到同一轴?

Python 如何将seaborn BoxPlot按顺序添加到同一轴?,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,有没有办法将多个seaborn箱线图按顺序添加到一个图形中 例如: 这给了我一系列的方块图 现在,有没有办法把两个时间序列像这样并排画在同一个图上?我想在函数中绘制它,该函数将具有make_new_plot布尔参数,用于分离从for循环中绘制的箱线图 如果我试着在同一个轴上调用它,它会给出重叠的图: 我知道可以连接数据帧并将连接的数据帧绘制成方框图,但我不希望此绘图函数返回任何数据帧 还有别的办法吗?也许可以通过某种方式操纵盒子的宽度和位置来实现这一点?事实上,我需要一个时间序列的箱线图&m

有没有办法将多个seaborn箱线图按顺序添加到一个图形中

例如:

这给了我一系列的方块图

现在,有没有办法把两个时间序列像这样并排画在同一个图上?我想在函数中绘制它,该函数将具有
make_new_plot
布尔参数,用于分离从for循环中绘制的箱线图

如果我试着在同一个轴上调用它,它会给出重叠的图:

我知道可以连接数据帧并将连接的数据帧绘制成方框图,但我不希望此绘图函数返回任何数据帧

还有别的办法吗?也许可以通过某种方式操纵盒子的宽度和位置来实现这一点?事实上,我需要一个时间序列的箱线图&matplotlib“positions”参数是故意不被seaborn支持的,这让我很难弄清楚如何做


注意它与eg不同,因为我希望按顺序打印,而不从plotting函数返回任何数据帧

如果希望在箱线图中有不同时间序列的色调嵌套,可以执行以下操作

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

n = 480
ts0 = pd.Series(np.random.randn(n), index=pd.date_range(start="2014-02-01", periods=n, freq="H"))
ts1 = pd.Series(np.random.randn(n), index=pd.date_range(start="2014-02-01", periods=n, freq="H"))
ts2 = pd.Series(np.random.randn(n), index=pd.date_range(start="2014-02-01", periods=n, freq="H"))

def ts_boxplot(ax, list_of_ts):
    new_list_of_ts = []
    for i, ts in enumerate(list_of_ts):
        ts = ts.to_frame(name='ts_variable')
        ts['ts_number'] = i
        ts['doy']=ts.index.dayofyear
        new_list_of_ts.append(ts)
    plot_data = pd.concat(new_list_of_ts)
    sns.boxplot(data=plot_data, x='doy', y='ts_variable', hue='ts_number', ax=ax)
    return ax

fig, ax = plt.subplots(figsize=(12,5))
ax = ts_boxplot(ax, [ts0, ts1, ts2])

如果希望在箱线图中有不同时间序列的色调嵌套,可以执行以下操作

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

n = 480
ts0 = pd.Series(np.random.randn(n), index=pd.date_range(start="2014-02-01", periods=n, freq="H"))
ts1 = pd.Series(np.random.randn(n), index=pd.date_range(start="2014-02-01", periods=n, freq="H"))
ts2 = pd.Series(np.random.randn(n), index=pd.date_range(start="2014-02-01", periods=n, freq="H"))

def ts_boxplot(ax, list_of_ts):
    new_list_of_ts = []
    for i, ts in enumerate(list_of_ts):
        ts = ts.to_frame(name='ts_variable')
        ts['ts_number'] = i
        ts['doy']=ts.index.dayofyear
        new_list_of_ts.append(ts)
    plot_data = pd.concat(new_list_of_ts)
    sns.boxplot(data=plot_data, x='doy', y='ts_variable', hue='ts_number', ax=ax)
    return ax

fig, ax = plt.subplots(figsize=(12,5))
ax = ts_boxplot(ax, [ts0, ts1, ts2])

第二套盒子应该放在哪里?每一个新的盒子在另一个旁边(如色调嵌套),或作为一个整体在右侧,位置52…71?@DizietAsahi每个时间步的盒子在另一个旁边。我在纯matplotlib中尝试了一种变通方法,但仍然想知道是否有一种更智能的方法可以做到这一点。第二组框应该放在哪里?每一个新的盒子在另一个旁边(如色调嵌套),或作为一个整体在右侧,位置52…71?@DizietAsahi每个时间步的盒子在另一个旁边。我设法在纯matplotlib中做了一个变通方法,但仍然想知道是否有一种更聪明的方法来做这件事。