首页 > 解决方案 > 从包含 PolyCollection 的 matplotlib 图形中提取数据

问题描述

我有一个matplotlib包含 PolyCollection 的图,该图类似于以下内容:

图提取

我想要的是提取颜色数据,即颜色数组(这里是黑色或白色)以及相应的xy值。

作为参考,用于生成图片的代码是:

import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection

verts = []

def polygon_under_graph(xlist, ylist):
    """
    Construct the vertex list which defines the polygon filling the space under
    the (xlist, ylist) line graph.  Assumes the xs are in ascending order.
    """
    return [(xlist[0], 0.), *zip(xlist, ylist), (xlist[-1], 0.)]


# Set up the x sequence
xs = np.linspace(0., 10., 26)

ys = np.random.rand(len(xs))
verts.append(polygon_under_graph(xs, ys))

fig, ax = plt.subplots(frameon=False)

poly = PolyCollection(verts,facecolors='k',edgecolors= 'k')
ax.add_collection(poly)

我尝试使用 poly.get_array()但没有返回任何内容。

关于如何进行的任何建议?

标签: pythonmatplotlib

解决方案


推荐阅读