0

I wanna try create location wise stacked bar chart I used following code

example of data 
 values are based on different different altitude on particular location  
location   N  T  S  Su
   1       25 20 30 40
   :
   :
   N

code:=

import numpy as np
import matplotlib.pyplot as plt  


N=np.array([20, 35, 30, 35, 27])
T=np.array([25, 32, 34, 20, 25])
S=np.array([30,5,35,10,20])
Su=np.array([30,5,35,10,20])


No =len(N)

ind = np.arange(No)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, N,   width, color='r')
p2 = plt.bar(ind, T, width, color='y',bottom=N)
p3=plt.bar(ind,S,width,color='g',bottom=N+T)
p4=plt.bar(ind,Su,width,color='w',bottom=N+T+S)

plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind+width/2., ('G1', 'G2', 'G3', 'G4', 'G5') )
plt.yticks(np.arange(0,200,10))
plt.legend( (p1[0], p2[0],p3[0],p4[0]), 

('Normal','Trapping','Super','Sub') )

plt.show()

It work on only for single value of T,N,S,Su but some time on particular lat,long value for T,N,Su,S will repeat that should be adjust in stack bar but I unable to do it please help me
value of T,N,S,Su grouped respectively have same colour based on grouped on single stack bar or all

example location  T    N    S     Su  N   T  S  N
            1     25   30  35     40  45  50 55 60
            :
            :
            N 

In this example N ,S has multiple value for single location on different altitude similarly there are N number of location and have similar condition please help me and give the solution

2
  • Could you provide "bad" input data (T, N, S, Su) in your code? Are you expecting length of bar for this non-scalar value [30, 45] to be max([30, 45]) or sum([30, 45]) or ...? Commented Oct 28, 2015 at 7:49
  • I edit My question please check the example at last Commented Oct 28, 2015 at 9:51

1 Answer 1

2

You could use the pandas module:

import pandas as pd

df = pd.DataFrame({'Location':['G1', 'G2', 'G3', 'G4', 'G5'],
                   'Normal':np.array([20, 35, 30, 35, 27]),
                   'Trapping':np.array([25, 32, 34, 20, 25]),
                   'Super':np.array([30,5,35,10,20]),
                   'Sub':np.array([30,5,35,10,20])})

df.plot(kind='bar', x='Location', stacked=True)
df2 = pd.concat([df, df[['Normal', 'Sub', 'Super']]], axis=1)

df2 = df2.groupby(level=0, axis=1).sum()
df2.plot(kind='bar', x='Location', stacked=True)

For your example this produces:

enter image description here

If we duplicate some columns pd.concat([df, df[['Normal', 'Sub', 'Super']]], axis=1) to make:

    Location    Normal  Sub Super   Trapping    Normal  Sub Super
0   G1  20  30  30  25  20  30  30
1   G2  35  5   5   32  35  5   5
2   G3  30  35  35  34  30  35  35
3   G4  35  10  10  20  35  10  10
4   G5  27  20  20  25  27  20  20

Then you can group and sum, then plot again:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.