简体   繁体   English

如何在python中的3行之间使用fill_between

[英]How to use fill_between between 3 lines in python

I have a list with my limit_up values, a list with the limit_down values, and a third list filled with '100' values (same size of the others).我有一个包含 limit_up 值的列表、一个包含 limit_down 值的列表和一个填充了“100”值的列表(与其他列表的大小相同)。 I'd like to make the area between limit_up and 100 to be green, and the area between (100 or limit_up) and limit_down to be red' like showed in the picture below.我想让 limit_up 和 100 之间的区域为绿色,(100 或 limit_up)和 limit_down 之间的区域为红色,如下图所示。

However, sometimes, as my limit_down list doesn't have any value below 100, the area needs to be fully green.但是,有时,由于我的 limit_down 列表没有任何低于 100 的值,因此该区域需要完全绿色。 And the same should happen when all my limit_up values are below 100, where the area needs to be fully red.当我所有的 limit_up 值都低于 100 时,应该会发生同样的情况,该区域需要完全红色。 Anybody could help?有人可以帮忙吗?

图片在这里

Data for figure on the left:左图数据:

limit_down = ['8.5', '37.5', '45.2', '51.9', '55.0', '55.2', '56.7']
limit_up = ['982.6', '393.3', '286.2', '260.4', '232.9', '200.0', '201.7']
reference = [100 100 100 100 100 100 100]

Data for figure on the right:右图数据:

limit_down = ['265.0', '649.1', '804.0', '895.1', '874.2', '957.9', '976.4']
limit_up = ['23815.5', '9043.4', '6932.4', '5805.6', '4510.5', '4317.5', '3963.5']
reference = [100 100 100 100 100 100 100]

fill_between and numpy's array filtering can be used as follows to create these plots: fill_between和 numpy 的数组过滤可以按如下方式用于创建这些图:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 35, 7)
limit_down_1 = np.array([8.5, 37.5, 45.2, 51.9, 55.0, 55.2, 56.7])
limit_up_1 = np.array([982.6, 393.3, 286.2, 260.4, 232.9, 200.0, 201.7])
reference = 100

limit_down_2 = np.array([265.0, 649.1, 804.0, 895.1, 874.2, 957.9, 976.4])
limit_up_2 = np.array([23815.5, 9043.4, 6932.4, 5805.6, 4510.5, 4317.5, 3963.5])

fig, axes = plt.subplots(ncols=2, figsize=(12, 4))
for ax, limit_up, limit_down in zip(axes, [limit_up_1, limit_up_2], [limit_down_1, limit_down_2]):
    ax.fill_between(x, np.maximum(reference, limit_down), limit_up, color='limegreen', alpha=0.3,
                    where=limit_up > reference, interpolate=True)
    ax.fill_between(x, limit_down, np.minimum(reference, limit_up), color='crimson', alpha=0.3,
                    where=limit_down < reference, interpolate=True)
    for y in (limit_up, limit_down):
        ax.plot(x[y <= reference], y[y <= reference], color='crimson')
        ax.plot(x[y >= reference], y[y >= reference], color='limegreen')
plt.show()

结果图

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

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