📜  如何在 Matplotlib 图上显示网格线?

📅  最后修改于: 2022-05-13 01:54:28.442000             🧑  作者: Mango

如何在 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 图中定义网格。语法由 -

示例 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()

输出