English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matplotlib Grid

The grid() function of the axes object sets the visibility of the grid in the figure to on or off. It can also display the main/secondary (or both) scales. In addition, you can set the color, linestyle, and linewidth properties in the grid() function.

Refer to the following example code -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
#! /usr/bin/env python
 #coding=utf-8
 import matplotlib.pyplot as plt
 import numpy as np
 import math
 # Display Chinese settings...
 plt.rcParams['font.sans-serif'] = ['SimHei']  # Step one (replace sans-serif font)
 plt.rcParams['axes.unicode_minus'] = False  # Step two (solve the problem of displaying negative signs of negative numbers on the coordinate axis)
 fig, axes = plt.subplots(1,3, figsize = (12,4))
 x = np.arange(1,11)
 axes[0].plot(x, x**3, 'g',lw=2)
 axes[0].grid(True)
 axes[0].set_title('Default Grid')
 axes[1].plot(x, np.exp(x), 'r')
 axes[1].grid(color='b', ls = '-. lw = 0.25)
 axes[1].set_title('Custom Grid')
 axes[2].plot(x,x)
 axes[2].set_title('Without Grid')
 fig.tight_layout()
 plt.show()

Execute the above example code to get the following result -