2017-03-08 83 views
3

下面的代码片段产生不可见图轴一条线,可见轴的正常情节:如何确定matplot lib轴是否已通过axes.axis('off')关闭?

import matplotlib.pyplot as plt 
fig, ax = plt.subplots(2) 
ax[0].plot([0, 1]) 
ax[0].set_xlabel('x1') 
ax[0].axis('off') 

ax[1].plot([1, 0]) 
ax[1].set_xlabel('x2') 

enter image description here

我想检测一个给定的axes实例的轴不论是一般的方法是可见的。我已经尝试了一些事情没有找到一种方法来区分哪些是可见的,从那些通过上述方法隐藏轴:

for i in range(2): 
    print('axes set', i, 
      ax[i].get_frame_on(), 
      ax[i].xaxis.get_visible(), 
      ax[i].xaxis.get_alpha()) 

结果:

('axes set', 0, True, True, None) 
('axes set', 1, True, True, None) 

正如你所看到的,没有的具有可见和不可见轴的子图的输出是不同的。

假设一组axes对象可能已经关闭,也可能没有关闭,请问我该如何判断哪些对象可见?

回答

3

您可以使用Axes对象的axison attribute来确定axes是打开还是关闭。

if ax.axison: 
    print 'on' 
else: 
    print 'off' 
+1

我觉得用这种方法遮蔽布尔数据类型有点危险,因为人们通常只是从答案中复制代码,然后陷入陷阱。 – ImportanceOfBeingErnest

+1

@ImportanceOfBeingErnest你是对的。没在想。现在修复。 – Suever

+0

别担心,我没有影响数据类型。 :) –