Skip to main contentSkip to solution

Which of the following is used to add gridlines to the background of a plot?

Solution

✅ Correct Option: 1

plt.grid() in Matplotlib turns on gridlines in the background of a plot. Calling plt.grid(True) or just plt.grid() shows them, and arguments such as axis='x', color and linestyle customise them.


Options 2 and 3: plt.xlabel() and plt.ylabel() only set the text labels of the axes, not gridlines.

Option 4: plt.yticks() sets the positions and labels of the y-axis ticks. Gridlines are drawn at tick positions, but this function does not add them.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question