Matplotlib Adding Grid Lines

Matplotlib Adding Grid Lines

Add Grid Lines to a Plot

With Pyplot, you can use the grid() function to add grid lines to the plot.

Example

Add grid lines to the plot:

Result:

Try it Yourself »


Specify Which Grid Lines to Display

You can use the axis parameter in the grid() function to specify which grid lines to display.

Legal values are: ‘x’, ‘y’, and ‘both’. Default value is ‘both’.

Example

Display only grid lines for the x-axis:

Result:

Try it Yourself »

Example

Display only grid lines for the y-axis:

Result:

Try it Yourself »


Set Line Properties for the Grid

You can also set the line properties of the grid, like this: grid(color = ‘color‘, linestyle = ‘linestyle‘, linewidth = number).

Example

Set the line properties of the grid:

Result:

Try it Yourself »

ArmenianEnglish