python - fill_between 总是低于绘图线

标签 python matplotlib

您好,我在 python 中使用 fill_between 命令,但 fill_between 似乎总是在线图下方。

看这个例子,alpha=1,所以不是不可见的。当它们位于 fill_between 位置时,不应看到所有线条。但我仍然看到它们。

iptyhon --pylab

plot([0,1],[1,1],'r')
plot([0,1],[2,2],'g')
plot([0,1],[0.5,0.5],'b')
plot([0,1],[3,3],'k')

fill_between([0,0.25],[0,3.5],facecolor='LightGreen')
fill_between([0.3,0.45],[0,3.5],facecolor='Salmon')
fill_between([0.5,0.7],[0,3.5],facecolor='LightBlue')
fill_between([0.75,0.95],[0,3.5],facecolor='LightYellow')

enter image description here

当我使用 alpha = 0.5 时,我仍然得到一些不正确的东西,正确的行为是每条线在与 fill_between 相同的位置时都会改变颜色,但你看到线的颜色没有改变...

plot([0,1],[1,1],'r')
plot([0,1],[2,2],'g')
plot([0,1],[0.5,0.5],'b')
plot([0,1],[3,3],'k')

fill_between([0,0.25],[0,3.5],facecolor='LightGreen',alpha=0.5)
fill_between([0.3,0.45],[0,3.5],facecolor='Salmon',alpha=0.5)
fill_between([0.5,0.7],[0,3.5],facecolor='LightBlue',alpha=0.5)
fill_between([0.75,0.95],[0,3.5],facecolor='LightYellow',alpha=0.5)

enter image description here

最佳答案

您可以使用 zorder kwarg 修复此问题:

plot([0,1],[1,1],'r',zorder=1)
plot([0,1],[2,2],'g',zorder=1)
plot([0,1],[0.5,0.5],'b',zorder=1)
plot([0,1],[3,3],'k',zorder=1)

fill_between([0,0.25],[0,3.5],facecolor='LightGreen',zorder=2)
fill_between([0.3,0.45],[0,3.5],facecolor='Salmon',zorder=2)
fill_between([0.5,0.7],[0,3.5],facecolor='LightBlue',zorder=2)
fill_between([0.75,0.95],[0,3.5],facecolor='LightYellow',zorder=2)

enter image description here

关于python - fill_between 总是低于绘图线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28542933/

相关文章:

python - html 模板中的 Django for 循环不显示

python - 从 pandas DataFrame 制作热图

python - Matplotlib - 绘制宽度等于一系列值的线,而不仅仅是一个值

python-3.x - 分割掩码的反转像素坐标

python - 如何在 NetworkX 中为 MultiGraph 绘制边标签?

python - 跨文件循环依赖的类型提示

python 从嵌套字典中获取所有值

python - 如何使用已经是 xml 格式的请求消息来提供泡沫

python - 如何将 geopandas 图提取为由像素数值组成的 numpy 数组?

matplotlib - 在 matplotlib 中使用换行符居中图例标题