matplotlib 中 fill_between 的 X-min 和 X-max

X-min and X-max from fill_between in matplotlib

有没有办法在执行 fill_between() 后从结果插值区域获取 x-max 和 x-min 坐标?

是。

fill_between() 函数 returns a matplotlib.collections.PolyCollection,它有一个名为 _paths 的属性。这包含图中存在的路径,坐标为 data-space。例如:

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> 
>>> x = np.linspace(0, 2*np.pi, 16)
>>> y = np.sin(x)
>>> 
>>> polys = plt.fill_between(x, y, where=(y > 0))
>>> 
>>> polys._paths
[Path(array([[0.6981317 , 0.        ],
    [0.6981317 , 0.64278761],
    [1.3962634 , 0.98480775],
    [2.0943951 , 0.8660254 ],
    [2.7925268 , 0.34202014],
    [2.7925268 , 0.        ],
    [2.7925268 , 0.        ],
    [2.0943951 , 0.        ],
    [1.3962634 , 0.        ],
    [0.6981317 , 0.        ],
    [0.6981317 , 0.        ]]), array([ 1,  2,  2, 2,  2,  2,  2,  2,  2,  2, 79], dtype=uint8))]

使用此路径裁剪图像图。