Thanks to @JohanC comment I was able to understand and solve the issue. It lies in fact in the method set_axisbelow() of the file axes/_base.py, the initial function is:
def set_axisbelow(self, b):
"""
Set whether axis ticks and gridlines are above or below most artists.
This controls the zorder of the ticks and gridlines. For more
information on the zorder see :doc:`/gallery/misc/zorder_demo`.
Parameters
----------
b : bool or 'line'
Possible values:
- *True* (zorder = 0.5): Ticks and gridlines are below all Artists.
- 'line' (zorder = 1.5): Ticks and gridlines are above patches
(e.g. rectangles, with default zorder = 1) but still below lines
and markers (with their default zorder = 2).
- *False* (zorder = 2.5): Ticks and gridlines are above patches
and lines / markers.
See Also
--------
get_axisbelow
"""
# Check that b is True, False or 'line'
self._axisbelow = axisbelow = validate_axisbelow(b)
zorder = {
True: 0.5,
'line': 1.5,
False: 2.5,
}[axisbelow]
for axis in self._axis_map.values():
axis.set_zorder(zorder)
self.stale = True
so we can see that this method sets to 0.5, 1.5, or 2.5 the zorder of the grid and ticks.
Hence, by modifying this function we can apply the zorder needed. I modified it as following:
def set_axisbelow(self, b):
"""
Set whether axis ticks and gridlines are above or below most artists.
This controls the zorder of the ticks and gridlines. For more
information on the zorder see :doc:`/gallery/misc/zorder_demo`.
Parameters
----------
b : bool or 'line'
Possible values:
- *True* (zorder = 0.5): Ticks and gridlines are below all Artists.
- 'line' (zorder = 1.5): Ticks and gridlines are above patches
(e.g. rectangles, with default zorder = 1) but still below lines
and markers (with their default zorder = 2).
- *False* (zorder = 2.5): Ticks and gridlines are above patches
and lines / markers.
See Also
--------
get_axisbelow
"""
# Check that b is True, False or 'line'
self._axisbelow = axisbelow = validate_axisbelow(b)
zorder = {
True: 0.5,
'line': 1.5,
False: 2.5,
}.get(axisbelow, axisbelow) # MODIF HERE
for axis in self._axis_map.values():
axis.set_zorder(zorder)
self.stale = True
There's also the need to modify the function validate_axisbelow so that it accepts number as argument (function located in file: rcsetup.py), here is how i made it:
def validate_axisbelow(s):
try:
return validate_bool(s)
except ValueError:
if isinstance(s, str):
if s == 'line':
return 'line'
elif isinstance(s, Number):
return s
raise ValueError(f'{s!r} cannot be interpreted as'
' True, False, "line", or zorder value')
Finally see my working code where i can adjust the grid zorder using in fact the custom set_axisbelow function:
import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.arange(4)
y1 = [5, 7, 3, 6]
y2 = [4, 6, 5, 7]
# Create figure with 2 subplots
fig, axs = plt.subplots(1, 2, figsize=(8, 4), dpi=120)
# Plot on subplot 1
axs[0].bar(x, y1, color='skyblue', zorder=127)
axs[0].set_title("Subplot 1")
axs[0].grid(True)
axs[0].set_axisbelow(128) # in fact zorder
# Plot on subplot 2
axs[1].bar(x, y2, color='salmon', zorder=76)
axs[1].set_title("Subplot 2")
axs[1].grid(True)
axs[1].set_axisbelow(75) # in fact zorder
plt.show()
and the result: