Search code examples
pythonpandasdataframeplotsubplot

Only the last subplot has minor gridlines


I would like to plot a subplot with the dataframe columns so I perform the following code:

fig, axes = plt.subplots(1, 2, figsize=(10,10), tight_layout=True)

plt.minorticks_on()
plt.grid(which='minor', linestyle='-', alpha=0.5)

ori_df.plot(x='Predicted', y='Actual', kind='scatter', ax=axes[0])
ori_df.plot(x='Predicted', y='# Difference', kind='scatter', ax=axes[1])

plt.show()

However, why does only the last plot have minor gridlines? subplots


Solution

  • you need to specify each axis:

    axes[0].grid(which='minor', linestyle='-', alpha=0.5)
    axes[1].grid(which='minor', linestyle='-', alpha=0.5)
    

    or:

    for axis in axes:
        axis.grid(which='minor', linestyle='-', alpha=0.5)
    

    Same for minorticks_on()