Is there any way of creating boxplot legend in matplotlib without using the return value of ax.boxplot() as in Adding a legend to a matplotlib boxplot with multiple plots on same axes?
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"))
for i, frame in zip(range(ts.index.dayofyear.nunique()),
ts.index.dayofyear.unique()):
plt.boxplot(ts[ts.index.dayofyear == frame], positions=[i], widths=0.9)
Above is just toy example code. In my project I actually call plt.boxplot() in the separate function each time (several calls of the function but the same axis is used) and I do not want to make any new return and parameter values.
Is there a way to create a legend for boxplots without using those return values as in other types of plots (by passing label=...)?
Since you are using a Pandas series, consider a Pandas plot solution that still interfaces with matplotlib. You will need to first upcast series to a data frame and assign needed hour and day indicator columns. With this approach, you can pass
axobject as an argument and use that for additional needs or use defaults withDataFrame.plot:Alternatively, if you need all boxplots in one plot,
pivotthe data for day columns: