📌  相关文章
📜  如何更改 Matplotlib 中的刻度数?

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

如何更改 Matplotlib 中的刻度数?

在本文中,我们将看到如何在Python的 matplotlib 中更改绘图上的刻度数。

方法 1:使用xticks()yticks()

xticks() 和 yticks() 是允许我们通过将值作为列表来自定义 x 刻度和 y 刻度的函数,我们还可以为刻度、事务和作为 **kwargs 的标签提供文本效果在刻度标签上。

例子:

在此示例中,我们将使用“内联”后端。 Matplotlib 图表将包含在笔记本中,位于代码旁边。然后我们给出 x 和 y 值来绘制并取简单的值,因为这样更容易理解这个概念。我们提供要在图中显示的标题、x-label、y-label。

Python3
# Importing the libraries
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
# %matplotlib inline
  
# Setting x and y values for the plot
x = [1, 2, 3, 4]
y = [7, 13, 24, 22]
  
# Initiating the plot
plt.plot(x, y)
plt.title("PLOT")
  
# Setting the x and y labels
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
  
# Showing the plot
plt.show()


Python3
# Setting x and y values for the plot
x = [1, 2, 3, 4]
y = [7, 13, 24, 22]
  
# Initiating the plot
plt.plot(x, y)
plt.title("PLOT")
  
# Setting the x and y labels
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
  
# Setting the number of ticks
plt.xticks([1, 2, 3, 4])
plt.yticks([7, 13, 24, 22])
  
# Showing the plot
plt.show()


Python3
# Setting x and y values for the plot
x = [1, 2, 3, 4]
y = [7, 13, 24, 22]
  
# Initiating the plot
plt.plot(x, y, color='Red')
plt.title("PLOT")
  
# Setting the x and y labels
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
  
# Setting the number of ticks
plt.locator_params(axis='both', nbins=4)
  
# Showing the plot
plt.show()


Python3
# Setting x and y values for the plot
x = [1, 2, 3, 4]
y = [7, 13, 24, 22]
  
# Initiating the plot
plt.plot(x, y)
plt.title("PLOT")
  
# Setting the x and y labels
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
  
# Setting the number of ticks
plt.xlim(0, 3)
plt.locator_params(axis='x', nbins=3)
  
# Showing the plot
plt.show()


输出:

现在,我们只需要 x 和 y 轴上的四个刻度;我们需要使用 xticks() 和 yticks()函数将我们需要的刻度作为列表提及。所以在这段代码中,我们建立了相同的绘图步骤。然后使用 xticks()函数,我们提到了值 (1,2,3,4),通过 yticks(),我们提到了值 (7,13,24,22)。输出中显示相同数量的刻度。

Python3

# Setting x and y values for the plot
x = [1, 2, 3, 4]
y = [7, 13, 24, 22]
  
# Initiating the plot
plt.plot(x, y)
plt.title("PLOT")
  
# Setting the x and y labels
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
  
# Setting the number of ticks
plt.xticks([1, 2, 3, 4])
plt.yticks([7, 13, 24, 22])
  
# Showing the plot
plt.show()

输出:

方法 2:使用locator_param()

Locator_params()函数可让我们更改图中的紧密度和刻度数。这是为了自定义 matplotlib 中的子图,我们需要将刻度包装得更紧和有限。因此,我们可以使用此函数来控制图上的刻度数。

现在,我们将两个轴上的刻度数限制为 4

例子:

在这个例子中,我们正在执行相同的步骤来制作一个绘图,并且我们只希望两边都有四个刻度。我们可以调用 locate_params()函数并提及轴,即 x 轴和 y 轴,并传入一个名为 nbins 的参数来提及我们想要在每个轴上的刻度数。

Python3

# Setting x and y values for the plot
x = [1, 2, 3, 4]
y = [7, 13, 24, 22]
  
# Initiating the plot
plt.plot(x, y, color='Red')
plt.title("PLOT")
  
# Setting the x and y labels
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
  
# Setting the number of ticks
plt.locator_params(axis='both', nbins=4)
  
# Showing the plot
plt.show()

输出:

方法 3:使用xlim()

使用这个 locate_params()函数,无论我们提到多少箱,我们将始终显示整个图,无论数量如何。不过,如果我们只想可视化 x 轴的前三个值,您可以使用此 xlim() 和 ylim()函数来设置绘图的 x 轴和 y 轴的限制,然后使用定位参数以减少刻度数。

注意:如果您在此函数后忘记使用 locate_param 不会减少刻度数,它仍会在您的绘图上显示所有不需要的刻度。因此,请确保在此函数之后使用 locate_params()函数

句法:

matplotlib.pyplot.xlim(*args, **kwargs)

现在,我们将尝试减少 x 轴上的刻度数,并尝试仅可视化绘图的前三个值。

例子:

与前面的示例一样,我们使用 locate_params() 来限制刻度数,但在此之前,我们将限制我们必须从头到尾可视化为元组的刻度数

Python3

# Setting x and y values for the plot
x = [1, 2, 3, 4]
y = [7, 13, 24, 22]
  
# Initiating the plot
plt.plot(x, y)
plt.title("PLOT")
  
# Setting the x and y labels
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
  
# Setting the number of ticks
plt.xlim(0, 3)
plt.locator_params(axis='x', nbins=3)
  
# Showing the plot
plt.show()

输出: