How to check if a subplot is empty in matplotlib

Viewed 638

I'm creating several subplots in matplotlib as:

fig, axes = plt.subplots(2, 2, figsize=(100, 85))
axes = axes.flatten()
'Other code for populating the subplots'

I want to remove the last plot, if it's empty. I tried this code:

if axes[-1] is None:
    axes[-1].set_visible(False)

The problem is that the axes elements are object of type AxesSubplot:

print(axes.flat[-1])

AxesSubplot(0.547727,0.125;0.352273x0.343182)

How can I solve the problem?

1 Answers

Solved:

if not axes[-1].lines: axes[-1].set_visible(False)
Related