Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get highest zorder object from matplotlib figure

Given a matplotlib figure or plot, how can I retrieve the value of the highest zorder object? For instance, retrieving the value of 5 in this example:

import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1)
ax.plot(np.arange(10), np.arange(10), zorder=5, linewidth=5)
ax.plot(np.arange(10), np.arange(10)[::-1], zorder=1, linewidth=5)
# implement: get_maximum_zorder(ax) 

enter image description here

like image 889
eqzx Avatar asked Sep 20 '26 01:09

eqzx


1 Answers

This can be done by accessing the axes's children, each of which has a zorder attribute.

max([_.zorder for _ in ax.get_children()])
like image 179
eqzx Avatar answered Sep 21 '26 15:09

eqzx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!