有没有一种方法可以依次向一个图形添加多个海洋箱形图?
以Time-series boxplot in pandas为例:
import pandas as pd
import numpy as np
import seaborn
import matplotlib.pyplot as plt
n = 480
ts = pd.Series(np.random.randn(n), index=pd.date_range(start="2014-02-01", periods=n, freq="H"))
fig, ax = plt.subplots(figsize=(12,5))
seaborn.boxplot(ts.index.dayofyear, ts, ax=ax)
这给了我一系列的箱线图吗?
现在,有没有办法同时绘制两个这样的时间序列?我想在具有make_new_plot
布尔参数的函数中绘制它,以将绘制的框线图从for循环中分离出来。
如果我只想在同一轴上调用它,它会给我重叠的图:
我知道可以串联数据框并一起绘制串联数据框的箱形图,但是我不想让此绘图功能返回任何数据框。
还有其他方法可以做到吗?也许可以通过某种方式操纵盒子的宽度和位置来实现这一目标?我需要有时间序列的boxplots和matplotlib“ positions”参数是有目的的事实,seaborn不支持该参数,这使我想起来如何操作有些棘手。
注意,它与例如Plotting multiple boxplots in seaborn?,因为我想按顺序绘制它,而不从绘制函数返回任何数据帧。
答案 0 :(得分:0)
如果您想在箱线图中嵌套不同时间序列的色调,您可以执行以下操作。
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])