Home > database >  Displaying a stacked bar graph from a few lists in python
Displaying a stacked bar graph from a few lists in python

Time:04-30

I have a few lists as below-

Clusters=['Cluster1', 'Cluster2', 'Cluster3', 'Cluster4', 'Cluster5', 'Cluster6', 'Cluster7']

clusterpoints= [[0, 2, 0, 5, 1, 0, 0, 0, 6, 0],
[0, 0, 5, 0, 0, 5, 1, 0, 1, 0],
[3, 0, 0, 1, 0, 6, 2, 0, 0, 0],
[1, 4, 0, 1, 0, 0, 0, 1, 2, 1],
[0, 2, 0, 5, 1, 0, 0, 0, 6, 0],
[0, 0, 5, 0, 0, 5, 1, 0, 1, 0],
[3, 0, 0, 1, 0, 6, 2, 0, 0, 0]]

xaxispoints=[ V1, V2, V3 ,V4 ,V5 , V6, V7, V8 , V9 ,V10 ]

How can i print a graph as shown in the example below ? - enter image description here

CodePudding user response:

Here is an approach using pandas:

import pandas as pd
import numpy as np

Clusters = ['Cluster1', 'Cluster2', 'Cluster3', 'Cluster4', 'Cluster5', 'Cluster6', 'Cluster7']
clusterpoints = [[0, 2, 0, 5, 1, 0, 0, 0, 6, 0],
                 [0, 0, 5, 0, 0, 5, 1, 0, 1, 0],
                 [3, 0, 0, 1, 0, 6, 2, 0, 0, 0],
                 [1, 4, 0, 1, 0, 0, 0, 1, 2, 1],
                 [0, 2, 0, 5, 1, 0, 0, 0, 6, 0],
                 [0, 0, 5, 0, 0, 5, 1, 0, 1, 0],
                 [3, 0, 0, 1, 0, 6, 2, 0, 0, 0]]
xaxispoints = ['V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9', 'V10']

df = pd.DataFrame(data=np.transpose(clusterpoints), columns=Clusters, index=xaxispoints)
df.plot.bar(stacked=True, rot=0)

pandas stacked bars

  • Related