Consider the following stackplot, created using pyplot from matplotlib using stackplot()
. There are two types of variables, earnings and expenses. Both of them are listed in the same legend.
I'd like to have two separate legends, one for earnings and one for expenses. There is already one example of how this can be done on this site, which however does not work with the PolyCollection
created by stackplot()
.
Here's a MWE with a single legend:
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
xVector = range(3)
earnings = {'fare': [2, 4, 6], 'tip': [1, 2, 3]}
expenses = {'maintenance': [-0.5, -1, -1.5], 'gas': [-1, -1.5, -2]}
ax.stackplot(xVector, earnings.values(), labels=earnings.keys())
ax.stackplot(xVector, expenses.values(), labels=expenses.keys())
plt.legend(loc='upper left')
plt.xlabel('time')
plt.ylabel('currency')
plt.show()
- How can I split up the above legend into two, one for earnings shown at the top left, and one for expenses, shown at the bottom left?
- Is there a way to ensure that the order of the legend entries matches the order in the stack plot (in my original example, this is the case for the expenses, but not for the earnings)?