Getting vertical gridlines to appear in line plot in matplotlib

前端 未结 6 1631
渐次进展
渐次进展 2020-12-01 05:41

I want to get both horizontal and vertical grid lines on my plot but only the horizontal grid lines are appearing by default. I am using a pandas.DataFrame from

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 06:18

    According to matplotlib documentation, The signature of the Axes class grid() method is as follows:

    Axes.grid(b=None, which='major', axis='both', **kwargs)
    Turn the axes grids on or off.

    which can be ‘major’ (default), ‘minor’, or ‘both’ to control whether major tick grids, minor tick grids, or both are affected.

    axis can be ‘both’ (default), ‘x’, or ‘y’ to control which set of gridlines are drawn.

    So in order to show grid lines for both the x axis and y axis, we can use the the following code:

    ax = plt.gca()
    ax.grid(which='major', axis='both', linestyle='--')
    

    This method gives us finer control over what to show for grid lines.

提交回复
热议问题