使用 MatplotLib python 的交互式堆积条形图
我需要使用 matplotlib 创建堆积条形图。每个条形图应该是我正在测量的参数的堆栈。但是,我希望它是交互式的或动态的,这样当我单击图例中的某个参数(A、B、C)时,它应该使该参数位于堆栈的底部,这样我们就可以有一个根据我们选择的参数,更好地比较不同候选者。
我从 matplotlib 中的示例中得到启发..这是我的代码
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()
谢谢...我希望我足够清楚
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
7 年后,但我想我可以使用 Python 3.6.0 和 Matplotlib 2.0.0 提供解决方案
7 years later, but I thought I'd provide a solution for this using Python 3.6.0 and Matplotlib 2.0.0