如何获取 matplotlib 图上的集合列表?

问题描述 投票:0回答:1

我正在尝试获取特定 matplotlib 图上的集合列表(散点图)。

对于法线图,可以通过以下方式完成

lines = [ line for axis in figure.axes for line in axis.get_lines()]

其中

axis.get_lines()
将为我提供与该轴关联的线对象列表。我如何对
PathCollections
或一般集合执行类似的操作?对象figure.axes似乎没有
.get_collections()
选项?

注意:我已经使用

axes.get_legend_handles_labels()
完成了此操作,并且只使用了手柄,但据我所知,只有当每个散点也有标签时,这才有效。

python matplotlib plot figure
1个回答
0
投票

我知道这是一个 7 年前的问题了,但我遇到了同样的问题。我尝试了@Matt的方法(

axis.get_lines()+axis.collections
),但是当在线图之间添加散点图时,图例文本颜色与图的颜色不匹配。我能够通过获取图例对象的所有句柄和文本来解决这个问题。接下来获取手柄颜色,然后将颜色设置为图例的文本。

handlers, labels = ax.get_legend_handles_labels()
for handle, label, text in zip(handlers, labels, self.legends.get_texts()):
      try:
          text.set_color(handle.get_color())
      except:
          text.set_color(handle.get_facecolors())`

© www.soinside.com 2019 - 2024. All rights reserved.