Python ProgrammingPython Programming

Stacked bar plot using Matplotlib

Legend with bubble size

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame([[10, 20, 30, 40], [7, 14, 21, 28], [5, 5, 0, 0]],
                  columns=['Apple', 'Orange', 'Banana', 'Pear'],
                  index=['Basket1', 'Basket2', 'Basket3'])

ax = df.plot(kind='bar', stacked=True)
ax.set_xlabel('DataFrame Values')
ax.set_ylabel('Basket')
plt.show()

The following is the output that will be obtained:


Stacked bar plot using Matplotlib