如何在 Matplotlib 图上显示网格线?
在本文中,我们将了解如何将网格线添加到 matplotlib 图形以及可在Python中使用的各种可配置参数。
示例:
使用样本数据创建 matplotlib 图。
Python
# Importing the library
import matplotlib.pyplot as plt
# Define X and Y data points
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
# Plot the graph using matplotlib
plt.plot(X, Y)
# Function to view the plot
plt.show()
Python
# Importing the library
import matplotlib.pyplot as plt
# Define X and Y data points
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
# Plot the graph using matplotlib
plt.plot(X, Y)
# Add gridlines to the plot
plt.grid(b=True)
# `plt.grid()` also works
# Function to view the plot
plt.show()
Python3
# Importing the library
import matplotlib.pyplot as plt
# Define X and Y data points
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
# Plot the graph using matplotlib
plt.plot(X, Y)
# Add gridlines to the plot
plt.grid(b=True, axis='x')
# `plt.grid()` also works
# Function to view the plot
plt.show()
Python3
# Importing the library
import matplotlib.pyplot as plt
# Define X and Y data points
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
# Plot the graph using matplotlib
plt.plot(X, Y)
# Add gridlines to the plot
plt.grid(b=True, axis='y')
# `plt.grid()` also works
# Function to view the plot
plt.show()
Python3
# Importing the library
import matplotlib.pyplot as plt
# Define X and Y data points
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
# Plot the graph using matplotlib
plt.plot(X, Y)
# Add gridlines to the plot
plt.grid(linestyle='--')
# `plt.grid()` also works
# Function to view the plot
plt.show()
Python3
# Importing the library
import matplotlib.pyplot as plt
# Define X and Y data points
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
# Plot the graph using matplotlib
plt.plot(X, Y)
# Add gridlines to the plot
plt.grid(linestyle='--', color='pink')
# `plt.grid()` also works
# Function to view the plot
plt.show()
输出
网格()
grid() 方法用于在 matplotlib 图中定义网格。语法由 -
Syntax:
matplotlib.pyplot.grid(b=None, which=’major’, axis=’both’, **kwargs)
Parameters:
- b: bool or None, optional: Whether to show the grid lines. If any kwargs are supplied, it is assumed you want the grid on and b will be set to True. If b is None and there are no kwargs, this toggles the visibility of the lines.
- which: {‘major’, ‘minor’, ‘both’}, optional: The grid lines to apply the changes on.
- axis: {‘both’, ‘x’, ‘y’}, optional: The axis to apply the changes on.
- **kwargs: Line2D properties: Define the line properties of the grid
示例 1:默认网格线
Python
# Importing the library
import matplotlib.pyplot as plt
# Define X and Y data points
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
# Plot the graph using matplotlib
plt.plot(X, Y)
# Add gridlines to the plot
plt.grid(b=True)
# `plt.grid()` also works
# Function to view the plot
plt.show()
输出
示例 2:将网格线应用于 x 轴
在我们只想查看垂直或水平网格线的情况下,我们可以使用 ` axis`参数。仅查看垂直网格线 –
语法:
plt.grid(b=True, axis='x')
Python3
# Importing the library
import matplotlib.pyplot as plt
# Define X and Y data points
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
# Plot the graph using matplotlib
plt.plot(X, Y)
# Add gridlines to the plot
plt.grid(b=True, axis='x')
# `plt.grid()` also works
# Function to view the plot
plt.show()
输出:
示例 3:将网格线应用于 y 轴
在我们只想查看垂直或水平网格线的情况下,我们可以使用 `axis` 参数。仅查看水平网格线 –
句法:
plt.grid(b=True, axis='y')
Python3
# Importing the library
import matplotlib.pyplot as plt
# Define X and Y data points
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
# Plot the graph using matplotlib
plt.plot(X, Y)
# Add gridlines to the plot
plt.grid(b=True, axis='y')
# `plt.grid()` also works
# Function to view the plot
plt.show()
输出:
示例 4:提供线型和线宽
我们已经看到网格线是实线。我们可以使用参数 ` linestyle ` 或 ` ls ` 更改它以查看不同的线条样式,例如破折号。同样,我们可以使用参数 ` linewidth`来改变网格线的粗细。这些参数作为关键字参数的一部分提供,可以在文档链接中查看。
提供线条样式 -
句法:
plt.grid(linestyle='--')
Python3
# Importing the library
import matplotlib.pyplot as plt
# Define X and Y data points
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
# Plot the graph using matplotlib
plt.plot(X, Y)
# Add gridlines to the plot
plt.grid(linestyle='--')
# `plt.grid()` also works
# Function to view the plot
plt.show()
输出:
.
示例 5:更改网格线的颜色
关键字参数 ` color`可用于为绘图定义不同的颜色。
句法:
plt.grid(linestyle, color)
Python3
# Importing the library
import matplotlib.pyplot as plt
# Define X and Y data points
X = [12, 34, 23, 45, 67, 89]
Y = [1, 3, 67, 78, 7, 5]
# Plot the graph using matplotlib
plt.plot(X, Y)
# Add gridlines to the plot
plt.grid(linestyle='--', color='pink')
# `plt.grid()` also works
# Function to view the plot
plt.show()
输出: