将集合添加到多个轴? [英] Adding a collection to multiple axis?

查看:63
本文介绍了将集合添加到多个轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 BrokenBarHCollection 添加到我的图形上的多个轴,如下所示:

I am trying to add a BrokenBarHCollection to multiple axis on my figure, as follows:

barcollection = collections.BrokenBarHCollection(...
ax1 = plt.subplot(211)
ax1.add_collection(barcollection)
ax2 = plt.subplot(212)
ax2.add_collection(barcollection)
plt.show()

照原样,该图仅显示第二个子图中的集合.如果我将ax2.add行注释掉,它仅在第一个子图中显示集合.在第3行和第4行之间再次声明barcollection,使其显示在两个子图中.为什么会这样?

As is, the figure only shows the collection in the second subplot. If I comment the ax2.add line out, it shows the collection only in the first subplot. Declaring the barcollection again between lines 3 and 4 makes it show up in both subplots. Why is this happening?

推荐答案

这是因为 matplotlib 对象知道它们附加到哪个图并且不会附加到多个图.

This is because the matplotlib objects know what plot they are attached to and will not attach to more than one.

如果您使用复制模块制作浅拷贝,那么您可以跨多个轴重用大多数数据结构.

If you use the copy module to make a shallow copy, then you can re-use most of the data structure across multiple axes.

import copy
bc2 = copy.copy(barcollection)
ax2.add_collection(bc2)

最近还有另一个问题,但是我很难找到它.

There was another question about this recently, but I am having trouble finding it.

这篇关于将集合添加到多个轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆