简体   繁体   English

使用`fill_between`在matplotlib中进行可变长度着色

[英]Variable length shading in matplotlib using `fill_between`

I'm trying to generate a variable length shading for the plot. 我正在尝试为该图生成一个可变长度的阴影。 Right now, I only have fixed length shading from the actual line. 现在,我与实际行之间只有固定长度的阴影

a    = np.linspace(1, 10)
loga = np.log(a)

_, ax = plt.subplots()

ax.plot(a, loga, lw=3, color='#27AE60', alpha=1)
ax.fill_between(a, loga+0.2, loga-0.2, color='#539caf', alpha=0.1)

ax.set_title("log plot")
plt.legend(["natural log"], loc="upper left")

The corresponding plot is: 相应的图是:

对数图

However, I'd like to get a plot like the one in below figure. 但是,我想得到下图所示的图。 src: MSc-thesis src:理学硕士论文

可变长度阴影

How can I modify my code to generate such a variable length shading? 如何修改代码以生成可变长度阴影? I mean the area to be shaded behind the line should increase as we go from left to right (ie as values in x-axis increases). 我的意思是,随着我们从左到右(即,随着x轴上的值增加), 该线后阴影区域应增加。 I would be grateful for any help. 我将不胜感激。 Merry Xmas! 圣诞快乐!

The answer is extremely obvious. 答案非常明显。 The arguments of fill_between are the x-values, the y-values of the top edge of the fill area, and those of the bottom edge. fill_between的参数是填充区域顶部边缘的x值,y值和底部边缘的y值。 Modifying those two will change the shading area: 修改这两个将更改阴影区域:

import matplotlib.pyplot as plt
import numpy as np

a    = np.linspace(1, 10)
loga = np.log(a)

_, ax = plt.subplots()

ax.plot(a, loga, lw=3, color='#27AE60', alpha=1)
ax.fill_between(a, loga+np.linspace(0,1,a.size), loga-np.linspace(0,1,a.size), color='#539caf', alpha=0.1)

ax.set_title("log plot")
plt.legend(["natural log"], loc="upper left")

plt.show()
plt.savefig('so.png')

在此处输入图片说明

Now, as to how to define these bounds... Of course this depends of the meaning you want these lines to have (ie your data). 现在,关于如何定义这些界限...当然,这取决于您希望这些行具有的含义(即您的数据)。 This is really problem specific, of which you mentioned nothing. 这确实是特定于问题的,您没有提及。 (And is probably not about programming, so off-site). (而且可能与编程无关,所以不在现场)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM