views:

36

answers:

1

Dear All,

I need to create a stacked bar chart using matplotlib. Each bar should be a stack of the parameters I am measuring. However, I want it to be interactive or dynamic so as when I click on one of the parameters (A,B,C) for example in the legend, it should make that parameter at the bottom of the stack so as we could have a better comparison between different candidates depending on the parameter we choose.

I got inspired from examples in matplotlib.. here is my code

import numpy as np
import matplotlib.pyplot as plt

N = 10  #could change

plt.figure()

A   = np.array([70, 88, 78, 93, 99, 58, 89, 66, 77, 78])
B = np.array([73, 65, 78, 87, 97, 57, 77, 88, 69, 78])
C = np.array([66, 98, 88, 67, 99, 88, 62, 70, 90, 73])


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

p1 = plt.bar(ind, A,width, color='r')
p2 = plt.bar(ind, B, width, color='y', bottom=A)
p3 = plt.bar(ind, C, width, color='b', bottom=A+B)


plt.ylabel('Scores')
plt.title('Index')

plt.xticks(ind+width/2., ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10'))#dynamic - fed

plt.yticks(np.arange(0,300,10))
plt.legend( (p1[0], p2[0], p3[0]), ('A','B','C') )
plt.grid(True)


plt.show()

Thank you... I hope I am clear enough

+1  A: 

Check out the demo code described here. It embeds a matplotlib bar chart into a PyQt GUI, allowing you to interact with the chart by changing its parameters dynamically and clicking on it to get events.

Eli Bendersky
Thank you Eli, that was very insightful..
Atlas