如何检测一个轴是否有一个双轴写在它上面?例如,如果在ax
下面给出,我如何发现它ax2
存在?
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax2 = ax.twinx()
Run Code Online (Sandbox Code Playgroud)
我不认为有任何内置功能,但您可能只是检查图中的任何其他轴是否与相关轴具有相同的边界框.这是一个快速的代码片段,可以执行此操作:
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
672 次 |
最近记录: |