Stacked bar Example#

Here shows how to draw stacked bar plot

import numpy as np
import pandas as pd

import milkviz as mv

First let’s create some random data#

data = pd.DataFrame(data=[["iPhone 12", "64G", 2200],
                          ["iPhone 12", "128G", 1200],
                          ["iPhone 12", "256G", 600],
                          ["iPhone 11", "64G", 1800],
                          ["iPhone 11", "128G", 1100],
                          ["iPhone 11", "256G", 700],
                          ["iPhone X", "64G", 3200],
                          ["iPhone X", "128G", 1200],
                          ["iPhone X", "256G", 600], ],
                    columns=['product', 'storage', 'sales'])
data
product storage sales
0 iPhone 12 64G 2200
1 iPhone 12 128G 1200
2 iPhone 12 256G 600
3 iPhone 11 64G 1800
4 iPhone 11 128G 1100
5 iPhone 11 256G 700
6 iPhone X 64G 3200
7 iPhone X 128G 1200
8 iPhone X 256G 600


Create the stacked_bar#

mv.stacked_bar(data, x="storage", y="sales", stacked="product", show_values=True)
plot stacked bar

Out:

<AxesSubplot:xlabel='storage', ylabel='sales'>

Normalize the data to 1#

mv.stacked_bar(data, x="storage", y="sales", stacked="product", percentage=True)
plot stacked bar

Out:

<AxesSubplot:xlabel='storage', ylabel='sales'>

Total running time of the script: ( 0 minutes 0.986 seconds)

Gallery generated by Sphinx-Gallery