Search code examples
pythonmatplotlibgraphswapstacked-bar-chart

Swipe or turn data for stacked bar chart in Matplotlib


I'm trying to create or generate some graphs in stacked bar I'm using this data:

    index   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17
0   No  94  123 96  108 122 106.0   95.0    124 104 118 73  82  106 124 109 70  59
1   Yes 34  4   33  21  5   25.0    34.0    5   21  9   55  46  21  3   19  59  41
2   Dont know   1   2   1   1   2   NaN NaN 1   4   2   2   2   2   2   2   1   7

Basically I want to use the columns names as x and the Yes, No, Don't know as the Y values, here is my code and the result that I have at the moment.

ax = dfu.plot.bar(x='index', stacked=True)

enter image description here

UPDATE:

enter image description here


Solution

  • Here is an example:

    data = [{0:1,1:2,2:3},{0:3,1:2,2:1},{0:1,1:1,2:1}]
    index = ["yes","no","dont know"]
    df = pd.DataFrame(data,index=index)
    df.T.plot.bar(stacked=True) # Note .T is used to transpose the DataFrame
    

    enter image description here