[英]fill_between() in barplot in matplotlib
import matplotlib.pyplot as plt
# define corner points
x=['a','b','c','d','e']
y=[12,10,5,1,4]
plt.bar(x,y,color='lightgray')
plt.fill_between(x,3,alpha=0.3,color='r')
I'd like to fill the empty space using fill_between()
as shown in the picture.我想使用fill_between()
填充空白区域,如图所示。
You can use axhspan
to have a horizontal bar over the complete plot width.您可以使用axhspan
在整个 plot 宽度上设置一个水平条。 Use zorder
to have it behind the bars.使用zorder
把它放在酒吧后面。
import matplotlib.pyplot as plt
x = ['a', 'b', 'c', 'd', 'e']
y = [12, 10, 5, 1, 4]
plt.bar(x, y, color='lightgray')
plt.axhspan(0, 3, alpha=0.3, color='r', zorder=0)
plt.show()
PS: Note that the call to fill_between
in the post doesn't fill between the bars. PS:请注意,帖子中对fill_between
的调用不会在条形之间填充。 It converts the x-values to numeric (being 0, 1, 2, ...
for a categorical axis) and uses a default y2=0
.它将 x 值转换为数字(分类轴为0, 1, 2, ...
)并使用默认值y2=0
。 So, internally equal to:所以,内部等于:
plt.fill_between(x=[0, 1, 2, 3, 4], y1=3, y2=0, ...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.