The pandas dataframe has the following form, whereYear
is the index:
A:Cat1 A:Cat2 B:Cat1 B:Cat2 B:Cat3
Year
1977 0.5 0.25 0.15 0.1 0.1
1981 0.2 NaN 0.40 0.1 0.2
1983 0.1 0.10 0.30 0.2 0.3
The important thing is that you have the same categories Cat1 and Cat2 in two different "super-categories", A and B. To plot the variations of all the categories, I use a stacked graph and use two different set of colors for each super-category. All those colors are saved in the list colors
.
What I am doing right now to draw the graph is (plt
is pyplot
):
plt.stackplot(data.index.values,data.fillna(0).T.values,colors=colors,labels=data.columns.values)
plt.legend(loc="best")
This gives the following with the previous data:
Now, what I would like to do is to avoid repeating the super-categories A and B in the legend, either by creating two distinct legends for each of the supercategories, or by having subheadings inside the same legend. I looked at this other question concerning subheadings, but the point is that I would like to be able to specify the breaking point between the two columns of the legend, so just specifying ncol=2
does not work because it does not break at the right point since I don't have the same number of categories in each «supercategory».