Python Matplotlib设置用于比较的默认栏

Python Matplotlib设置用于比较的默认栏,python,matplotlib,Python,Matplotlib,我有一个条形图,我想显示实际/预期之间的比较。 例如: 我从其他人那里获取了这张图,但基本上我想要的是背景中的“默认灰色条”,同时以绿色显示实际数据,而不是: 我可以使用什么方法?您可以在堆栈中的顶部条始终具有相同的最大值的位置进行堆栈。我修改了这个例子 import numpy as np import matplotlib.pyplot as plt # data to plot data1 = np.array((50, 35, 30, 33, 60)) data2 = np.ar

我有一个条形图,我想显示实际/预期之间的比较。 例如:

我从其他人那里获取了这张图,但基本上我想要的是背景中的“默认灰色条”,同时以绿色显示实际数据,而不是:

我可以使用什么方法?

您可以在堆栈中的顶部条始终具有相同的最大值的位置进行堆栈。我修改了这个例子

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()