I've got a library function which plots data into a pyplot figure containing several subplots. I just added grid lines to all of the subplots but they overlay the actual data but I would prefer them to be in the background. I've tried changing the order in which the plotting and ax.plot() ax.grid() commands are executed but that has no influence. Is there a way to force the grid into the background?
Related bonus question: I'm also using axhline to designate the x=0 line but it always assumes the grid colour even though it is being specified in a different one ...
The way the code currently works:
def plot_the_things(fig=None):
# in case no figure is provided, make a new one,
# otherwise add to the existing one
plot_fig=fig if fig else plt.figure()
#[...some calculations of data ...]
plot_ax1 = plot_fig.add_subplot(3,3,1)
plot_ax1.axhline(y=0, ls='-', color='0.5')
plot_ax1.plot(self.diff_3[:,0],self.diff_3[:,1])
# [...setting labels, adapt axes limits in case the new data needs wider ones..]
plot_ax1.grid(b=True, which='major', axis='both', c='0.75', ls='-', linewidth=1)
# this is repeated in similar fashion for the other axes -- there are 9 of
# them, each plotting something different in a different axes
This function is called several times over. More precisely: It's actually part of a class. I have multiple instances of this class and call all of them, passing in the same figure object. Each instance then draws its own data, which works fine, and even the axhline()
was shown properly (below the data!) but after I put in the command to add the grid, it always shows up on top of the data and covers the axhline, which is annoying.
... any way to fix this?
(I think I could and maybe should also move all the things that only need to run once to a place where they aren't repeatedly executed but time and mental resources are scant right now, so I went with the quickest way that worked... but I wouldn't expect this to change anything)