如何使用 matplotlib 填充或遮蔽 Python 中堆叠条形图中两个对应点之间的区域?
How to fill or shade area between two corresponding points in stacked bar plots in Python using matplotlib?
我有一个数据框 df
如下所示:
A B
X 5 7
Y 10 5
df.to_dict()
给出以下内容:
{'A': {'X': 5, 'Y': 10}, 'B': {'X': 7, 'Y': 5}}
我使用
创建了一个堆积条形图
df.plot(kind = "bar", stacked = True)
看起来如下:
我想在 X 和 Y 条中对 A 之间的区域进行阴影处理,对 B 也一样。阴影区域反映了 A 和 B 的值在 X 和 Y 之间的变化情况。它应该如下所示:
如何在 Python 中使用 matplotlib 填充这两个堆叠条形图之间的区域,保持条形图的原始结构不变?
这是使用fill_between
.
的方法
ax = df.plot(kind = "bar", stacked = True)
plt.fill_between(x = [ax.patches[0].get_x() + ax.patches[0].get_width(),
ax.patches[1].get_x()],
y1 = 0,
y2 = [ax.patches[0].get_y() + ax.patches[0].get_height(),
ax.patches[1].get_y() + ax.patches[1].get_height()],
color = ax.patches[0].get_facecolor(), alpha=0.5)
plt.fill_between(x = [ax.patches[2].get_x() + ax.patches[2].get_width(),
ax.patches[3].get_x()],
y1 = [ax.patches[0].get_y() + ax.patches[0].get_height(),
ax.patches[1].get_y() + ax.patches[1].get_height()],
y2 = [ax.patches[2].get_y() + ax.patches[2].get_height(),
ax.patches[3].get_y() + ax.patches[3].get_height()],
color = ax.patches[2].get_facecolor(), alpha=0.5)
plt.plot()
好的。我自己使用 ax.fill_between()
找到了一个简单的方法。对于 x,我指定 [0.25, 0.75]。 0.25 指的是 X 条的右边缘,0.75 指的是 Y 条的左边缘。X 轴上带有刻度的位置 X 和 Y 分别为 0 和 1。
对于y1和y2,我分别指定了要填充的下边缘和上边缘的y坐标。
fig, ax = plt.subplots()
ax = df.plot(kind = "bar", stacked = True, ax = ax, color = ["blue","orange"])
ax.fill_between(x =[0.25,0.75], y1 = [0, 0], y2 = [5, 10], color = "blue", alpha = 0.5)
ax.fill_between(x =[0.25, 0.75], y1 = [5, 10], y2 = [12, 15], color = "orange", alpha = 0.5)
plt.show()
我得到了如图所示的东西:
这是另一个 fill_between 更通用的方法:
# loop through the bars to get the bottom and top points
bottoms = []
tops = []
for patch in ax.patches:
x,y = patch.get_xy()
w,h = patch.get_width(), patch.get_height()
bottoms += [(x,y), (x+w, y)]
tops += [(x, y+h), (x+w, y+h)]
# convert to numpy for easy slicing
tops = np.array(tops)
bottoms = np.array(bottoms)
# extract the x coordinates
x = np.unique(bottoms[:,0])
num_x = len(x)
# fill between each bottom and top pairs
for i in range(0, len(bottoms), num_x):
plt.fill_between(x, tops[i:i+num_x, 1], bottoms[i:i+num_x, 1], alpha=0.5)
输出:
我有一个数据框 df
如下所示:
A B
X 5 7
Y 10 5
df.to_dict()
给出以下内容:
{'A': {'X': 5, 'Y': 10}, 'B': {'X': 7, 'Y': 5}}
我使用
创建了一个堆积条形图df.plot(kind = "bar", stacked = True)
看起来如下:
我想在 X 和 Y 条中对 A 之间的区域进行阴影处理,对 B 也一样。阴影区域反映了 A 和 B 的值在 X 和 Y 之间的变化情况。它应该如下所示:
如何在 Python 中使用 matplotlib 填充这两个堆叠条形图之间的区域,保持条形图的原始结构不变?
这是使用fill_between
.
ax = df.plot(kind = "bar", stacked = True)
plt.fill_between(x = [ax.patches[0].get_x() + ax.patches[0].get_width(),
ax.patches[1].get_x()],
y1 = 0,
y2 = [ax.patches[0].get_y() + ax.patches[0].get_height(),
ax.patches[1].get_y() + ax.patches[1].get_height()],
color = ax.patches[0].get_facecolor(), alpha=0.5)
plt.fill_between(x = [ax.patches[2].get_x() + ax.patches[2].get_width(),
ax.patches[3].get_x()],
y1 = [ax.patches[0].get_y() + ax.patches[0].get_height(),
ax.patches[1].get_y() + ax.patches[1].get_height()],
y2 = [ax.patches[2].get_y() + ax.patches[2].get_height(),
ax.patches[3].get_y() + ax.patches[3].get_height()],
color = ax.patches[2].get_facecolor(), alpha=0.5)
plt.plot()
好的。我自己使用 ax.fill_between()
找到了一个简单的方法。对于 x,我指定 [0.25, 0.75]。 0.25 指的是 X 条的右边缘,0.75 指的是 Y 条的左边缘。X 轴上带有刻度的位置 X 和 Y 分别为 0 和 1。
对于y1和y2,我分别指定了要填充的下边缘和上边缘的y坐标。
fig, ax = plt.subplots() ax = df.plot(kind = "bar", stacked = True, ax = ax, color = ["blue","orange"]) ax.fill_between(x =[0.25,0.75], y1 = [0, 0], y2 = [5, 10], color = "blue", alpha = 0.5) ax.fill_between(x =[0.25, 0.75], y1 = [5, 10], y2 = [12, 15], color = "orange", alpha = 0.5) plt.show()
我得到了如图所示的东西:
这是另一个 fill_between 更通用的方法:
# loop through the bars to get the bottom and top points
bottoms = []
tops = []
for patch in ax.patches:
x,y = patch.get_xy()
w,h = patch.get_width(), patch.get_height()
bottoms += [(x,y), (x+w, y)]
tops += [(x, y+h), (x+w, y+h)]
# convert to numpy for easy slicing
tops = np.array(tops)
bottoms = np.array(bottoms)
# extract the x coordinates
x = np.unique(bottoms[:,0])
num_x = len(x)
# fill between each bottom and top pairs
for i in range(0, len(bottoms), num_x):
plt.fill_between(x, tops[i:i+num_x, 1], bottoms[i:i+num_x, 1], alpha=0.5)
输出: