gpt4 book ai didi

python - 如何检测是否为matplotlib轴生成了双轴

转载 作者:太空狗 更新时间:2023-10-30 02:28:36 25 4
gpt4 key购买 nike

如何检测轴上是否写有双轴?例如,如果给定下面的 ax,我如何发现 ax2 存在?

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax2 = ax.twinx()

最佳答案

我不认为有任何内置功能可以执行此操作,但您可能只检查图中的任何其他轴是否具有与相关轴相同的边界框。这是将执行此操作的快速片段:

def has_twin(ax):
for other_ax in ax.figure.axes:
if other_ax is ax:
continue
if other_ax.bbox.bounds == ax.bbox.bounds:
return True
return False

# Usage:

fig, ax = plt.subplots()
print(has_twin(ax)) # False

ax2 = ax.twinx()
print(has_twin(ax)) # True

关于python - 如何检测是否为matplotlib轴生成了双轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36209575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com