'I want to manually change my legends in matplotlib, but I always mess up

I have issues showing two legends underneath each other. Either they are overlapping or only one shows up. I don't want to use the default legend. I tried 3 approaches and none are good. I am out of ideas.

plt.figure()
ax = dftotal[['PriceVIX','PriceSPX']].dropna().plot(secondary_y=['PriceSPX'], figsize=(8, 4),label=['1','2'])
ax.set_xlabel("Date")
ax.set_ylabel('Price VIX')
ax.right_ax.set_ylabel('Price SP500')
ax.legend(labels=['1','2'])

enter image description here

My second code doesn't work either:

a1 = dftotal['PriceVIX'].dropna().plot(figsize=(8, 4))
a = dftotal['PriceSPX'].dropna().plot(secondary_y = True)
a1.set_xlabel("Date")
a1.set_ylabel("Price VIX")
a.set_ylabel("Price SP500")
a.legend(["SP500"])
a1.legend(["VIX"])

enter image description here

And my third attempt looks ugly with this "Rights" in the legend:

plt.figure()
ax = dftotal[['PriceVIX','PriceSPX']].dropna().plot(secondary_y=['PriceSPX'], figsize=(8, 4),label=['1','2'])
ax.set_xlabel("Date")
ax.set_ylabel('Price VIX')
ax.right_ax.set_ylabel('Price SP500')

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source