Matplotlib设置比较默认栏

时间:2014-03-26 18:52:41

标签: python matplotlib

我有一个条形图,我希望显示实际/预期之间的比较。 例如:Green bar/ Grey background

我从其他人那里拿走了这张图,但基本上我想要的是"默认的灰色栏"在后台同时以绿色显示实际数据而不是: Green bar/ No background

我可以使用哪种方法?

1 个答案:

答案 0 :(得分:1)

您可以堆叠bar chart,其中堆栈中的顶部栏始终具有相同的最大值。我修改了示例here

import numpy as np
import matplotlib.pyplot as plt

# data to plot
data1  = np.array((50, 35, 30, 33, 60))
data2 = np.array((25, 32, 34, 20, 5))

# find the size of the grey bar to be plotted at the top
ymax = 100
s = np.shape(data1)
n = s[0]
top = ymax*np.ones((n,)) - data1 - data2

ind = np.arange(n) # the x locations for the groups
width = 0.35 # the width of the bars

p1 = plt.bar(ind, data1, width, color='g')
p2 = plt.bar(ind, data2, width, color='y', bottom=data1)
p3 = plt.bar(ind, top, width, color ='grey', bottom=data2 + data1)

plt.show()

stacked bar chart with grey top