'How can I plot something above a negative stacked bar chart using Matplotlib Python?

wish you a good day ! I must plot something like that using matplotlib : enter image description here

The standard ax.bar() function does not easily plot stacked bar charts with negative values, that's why I used dataframe.plot(stacked = True) to plot this bar. The problem is that I need to plot a line plot above this bar chart. This line should use the second vertical axis, so I need to get the current matplotlib axis object used by my dataframe.plot(stacked = True) function, I need to twinx() it and then plot the line on the twined axis.

However these 3 things do not work :

enter image description here which gives : enter image description here

enter image description here which gives : enter image description here

and

enter image description here which gives : enter image description here

I dont know what is going wrong, I know the problem stems from the dataframe.plot(stacked = True) function which is the only one that stacks easily bar charts with negative values.



Sources

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

Source: Stack Overflow

Solution Source