Python study notes for the 64th day (Matplotlib grid lines)

Matplotlib gridlines

We can use the grid() method in pyplot to set the grid lines in the chart.

The grid() method syntax is as follows:

matplotlib.pyplot.grid(b=None, which='major', axis='both', )

Parameter Description:

  • b: Optional, the default is None, a Boolean value can be set, true means to display grid lines, false means not to display, if the **kwargs parameter is set, the value is true.
  • which: optional, optional values ​​​​are 'major', 'minor' and 'both', the default is 'major', indicating the grid lines to apply changes to.
  • axis: Optional, set which direction to display the grid lines. It can be 'both' (default), 'x' or 'y', which respectively represent two directions, x-axis direction or y-axis direction.
  • **kwargs: Optional, set the grid style, which can be color='r', linestyle='-' and linewidth=2, which represent the color, style and width of the grid lines respectively.

Ordinary grid lines

The following example adds a simple grid line with default parameters:

# 实例 1
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])


plt.title("RUNOOB grid() Test")
plt.xlabel("x - label")
plt.ylabel("y - label")

plt.plot(x, y)

plt.grid()

plt.show()

The following example adds a simple grid line, uses x as the axis parameter, and sets the x-axis direction to display the grid lines:

# 实例 2
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])


plt.title("grid() Test")
plt.xlabel("x - label")
plt.ylabel("y - label")

plt.plot(x, y)

plt.grid(axis='x') # 设置 y 就在轴方向显示网格线

plt.show()

style grid lines

grid(color = 'color', linestyle = 'linestyle', linewidth = number)

Parameter Description:

  • color: 'b' blue, 'm' magenta, 'g' green, 'y' yellow, 'r' red, 'k' black, 'w' white, 'c' turquoise, '#008000' RGB Color string.
  • linestyle: '‐' solid line, '‐‐' dashed line, '‐.' dotted line, ':' dashed line.
  • linewidth: Set the width of the line, you can set a number.

The following example adds a simple grid line and sets the grid line style. The format is as follows:

# 实例 3
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])


plt.title("grid() Test")
plt.xlabel("x - label")
plt.ylabel("y - label")

plt.plot(x, y)

plt.grid(color = 'r', linestyle = '--', linewidth = 0.5)

plt.show()

postscript

What you are learning today is Python Matplotlib grid lines. Have you learned it? A summary of today’s learning content:

  1. Matplotlib gridlines
  2. Ordinary grid lines
  3. style grid lines

Supongo que te gusta

Origin blog.csdn.net/qq_54129105/article/details/132394297
Recomendado
Clasificación