簡體   English   中英

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

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

我有一個包含 limit_up 值的列表、一個包含 limit_down 值的列表和一個填充了“100”值的列表(與其他列表的大小相同)。 我想讓 limit_up 和 100 之間的區域為綠色,(100 或 limit_up)和 limit_down 之間的區域為紅色,如下圖所示。

但是,有時,由於我的 limit_down 列表沒有任何低於 100 的值,因此該區域需要完全綠色。 當我所有的 limit_up 值都低於 100 時,應該會發生同樣的情況,該區域需要完全紅色。 有人可以幫忙嗎?

圖片在這里

左圖數據:

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]

右圖數據:

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和 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