【问题标题】:How can I determine if matplot lib axes have been turned off with axes.axis('off')?如何确定 matplotlib 轴是否已使用 axes.axis('off') 关闭?
【发布时间】:2017-03-08 00:10:58
【问题描述】:

以下代码 sn-p 生成一条没有可见绘图轴的线和一条带有可见轴的普通绘图:

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')

我想要一种检测给定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 对象,这些对象可能已被 .axis('off') 关闭或未关闭,我如何判断哪些是可见的?

【问题讨论】:

    标签: python python-2.7 matplotlib plot


    【解决方案1】:

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

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

    【讨论】:

    • 我认为以这种方式隐藏布尔数据类型有点危险,因为人们经常只是简单地从答案中复制代码然后陷入陷阱。
    • @ImportanceOfBeingErnest 是的,你是对的。没有想。现已修复。
    • 别担心,我没有隐藏数据类型。 :)
    猜你喜欢
    • 2020-01-30
    • 1970-01-01
    • 2014-01-11
    • 2020-01-26
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多