0

I am created a set of subplots, and I want to show light gray minor grid lines on my log y-axis. This code works for some of the plots, but not all. I can't figure out why it doesn't work for all plots.

It seems to be somewhat related to the plot title; if I comment it out, 11 of the 12 plots are correct.

I want to keep the 'seaborn-whitegrid' plot style, just with this modification of adding the minor grid lines to the y-axis.

import matplotlib.pyplot as plt
%matplotlib inline 
from matplotlib.ticker import ScalarFormatter
plt.style.use('seaborn-whitegrid')

fig, axes = plt.subplots(3,4, figsize=(8.5,5.5), constrained_layout=True)

counter = 0
for ax in axes.flat:
    id1 = mysummary.id1[counter]
    df_subset = df[df.ID == id1]
    
    ax.hist(df_subset.x, density=False,
            edgecolor='black', color='lightgray',
            cumulative=False, orientation='vertical')
    ax.set_title('my long \n title', fontsize=8)
    ax.set_xlabel('x', fontsize=8)
    ax.set_ylabel('Count', fontsize=8)
    ax.set_yscale('log')
    ax.set_ylim(ymin = 1)
    ax.set_xlim(xmin = 0)
    ax.grid(b=True, which='minor', axis='y', color='#999999', alpha=0.2)
    ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))
    ax.locator_params(axis='x', nbins=4)
    ax.xaxis.set_tick_params(labelsize='small')
    ax.yaxis.set_tick_params(labelsize='small')
    counter += 1

enter image description here

a11
  • 3,122
  • 4
  • 27
  • 66
  • Between `ax.grid` and `ax.yaxis` try: `ax.minorticks_on`.. Please see: https://stackoverflow.com/questions/19940518/cannot-get-minor-grid-lines-to-appear-in-matplotlib-figure – David Erickson Jan 06 '21 at 02:36
  • @DavidErickson just tried it, same results. I also tried putting `ax.minorticks_on` before `ax.grid()` thanks thought for the good idea, it seems like that should have worked – a11 Jan 06 '21 at 15:24

0 Answers0