Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if matplot lib axes have been turned off with axes.axis('off')?

The following code snippet produces a line with no visible plot axes and a normal plot with visible axes:

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

I would like a general way to detect whether the axes of a given axes instance are visible. I have tried a few things without finding a way to distinguish axes which are visible from those which are hidden by the above method:

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

Results:

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

As you can see, none of the outputs are different between the subplots with visible and invisible axes.

Given a set of axes objects which may or may not have been turned off with .axis('off'), how can I tell which ones are visible?

like image 568
EL_DON Avatar asked Sep 17 '25 20:09

EL_DON


1 Answers

You can use the axison attribute of the Axes object to determine whether the axes is turned on or off.

if ax.axison:
    print 'on'
else:
    print 'off'
like image 67
Suever Avatar answered Sep 20 '25 13:09

Suever