📌  相关文章
📜  如何从 Matplotlib 图中删除刻度?

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

如何从 Matplotlib 图中删除刻度?

Matplotlib 是一个Python库,它为我们提供了各种函数,我们可以使用这些函数绘制数据并以图形方式对其进行可视化。但是通常使用 Matplotlib 库绘制图形时,我们会在绘图中得到刻度,默认情况下,它们会在图的 x 轴和 y 轴的两侧进行标记。有时我们不想在绘图中显示这些刻度。 Matplotlib.pyplot 库为我们提供了一个tick_params()方法,我们可以使用该方法手动删除这些刻度。

tick_params()函数接受一些带有布尔值的属性,这些属性可用于删除绘图上的刻度和标签。默认情况下,此属性中的值设置为 True,

让我们通过一个例子来理解这一点:

Python
# importing matplotlib library
import matplotlib.pyplot as plt
  
# making points to be plotted on x-axis and y-axis
X_axis = [i for i in range (10, 110, 10)]
Y_axis = [2*j+5 for j in range (10, 110, 10)]
  
# printing points to be plotted on the x and y-axis
print("Points on x-axis are: ",X_axis)
print("Points on y-axis are: ",Y_axis)
  
# Creating a default plot
plt.figure(figsize=(8,6))
plt.plot(X_axis,Y_axis)
plt.scatter(X_axis,Y_axis)
plt.show()


Python
# importing matplotlib library
import matplotlib.pyplot as plt
  
# making points to be plotted on x-axis and y-axis
X_axis = [i for i in range (10, 110, 10)]
Y_axis = [2*j+5 for j in range (10, 110, 10)]
  
# printing points to be plotted on the x and y-axis
print("Points on x-axis are: ", X_axis)
print("Points on y-axis are: ", Y_axis)
  
plt.figure(figsize = ( 8, 6))
plt.tick_params(left = False)
plt.plot(X_axis, Y_axis)
plt.scatter(X_axis, Y_axis)
plt.show()


Python
# importing matplotlib library
import matplotlib.pyplot as plt
  
# making points to be plotted on x-axis and y-axis
X_axis = [i for i in range (10, 110, 10)]
Y_axis = [2*j+5 for j in range (10, 110, 10)]
  
# printing points to be plotted on the x and y-axis
print("Points on x-axis are: ", X_axis)
print("Points on y-axis are: ", Y_axis)
  
plt.figure(figsize = (8, 6))
plt.tick_params(bottom = False)
plt.plot(X_axis, Y_axis)
plt.scatter(X_axis, Y_axis)
plt.show()


Python
# importing matplotlib library
import matplotlib.pyplot as plt
  
# making points to be plotted on x-axis and y-axis
X_axis = [i for i in range (10, 110, 10)]
Y_axis = [2*j+5 for j in range (10, 110, 10)]
  
# printing points to be plotted on the x and y-axis
print("Points on x-axis are: ", X_axis)
print("Points on y-axis are: ", Y_axis)
  
plt.figure(figsize = (8,6))
plt.tick_params(left = False, bottom = False)
plt.plot(X_axis, Y_axis)
plt.scatter(X_axis, Y_axis)
plt.show()


Python3
# importing matplotlib library
import matplotlib.pyplot as plt
  
# making points to be plotted on x-axis and y-axis
X_axis = [i for i in range (10, 110, 10)]
Y_axis = [2*j+5 for j in range (10, 110, 10)]
  
# printing points to be plotted on the x and y-axis
print("Points on x-axis are: ", X_axis)
print("Points on y-axis are: ", Y_axis)
  
plt.figure(figsize = (8,6))
plt.tick_params(left = False, right = False , labelleft = False ,
                labelbottom = False, bottom = False)
  
plt.plot(X_axis, Y_axis)
plt.scatter(X_axis, Y_axis)
plt.show()


输出:



使用 matplotlib 制作的默认图,包括刻度

通过查看上图,我们可以观察到默认情况下 Matplotlib 在 x 和 y 轴上标记刻度。

案例 1.1:当我们想要删除单个轴(此处为 y 轴)上的刻度时:

要删除 y 轴上的刻度,tick_params() 方法有一个名为left的属性,我们可以将其值设置为False并将其作为参数传递给 tick_params()函数。它会删除 y 轴上的刻度。

Python

# importing matplotlib library
import matplotlib.pyplot as plt
  
# making points to be plotted on x-axis and y-axis
X_axis = [i for i in range (10, 110, 10)]
Y_axis = [2*j+5 for j in range (10, 110, 10)]
  
# printing points to be plotted on the x and y-axis
print("Points on x-axis are: ", X_axis)
print("Points on y-axis are: ", Y_axis)
  
plt.figure(figsize = ( 8, 6))
plt.tick_params(left = False)
plt.plot(X_axis, Y_axis)
plt.scatter(X_axis, Y_axis)
plt.show()

输出:

使用 tick_params() 方法删除 y 轴上的刻度后绘制的图

案例 1.2:当我们想要删除单个轴(此处为 x 轴)上的刻度时:



要删除 x 轴上的刻度,tick_params() 方法接受一个名为 bottom 的属性,我们可以将其值设置为False并将其作为参数传递给 tick_params()函数。它会删除 x 轴上的刻度。

Python

# importing matplotlib library
import matplotlib.pyplot as plt
  
# making points to be plotted on x-axis and y-axis
X_axis = [i for i in range (10, 110, 10)]
Y_axis = [2*j+5 for j in range (10, 110, 10)]
  
# printing points to be plotted on the x and y-axis
print("Points on x-axis are: ", X_axis)
print("Points on y-axis are: ", Y_axis)
  
plt.figure(figsize = (8, 6))
plt.tick_params(bottom = False)
plt.plot(X_axis, Y_axis)
plt.scatter(X_axis, Y_axis)
plt.show()

输出:

使用 tick_params() 方法删除 x 轴上的刻度后绘制的图

案例 2:使用 tick_params() 方法在两个轴上删除这些刻度:

要同时删除 x 轴和 y 轴上的刻度,我们可以同时传递leftright属性,将其值设置为False并将其作为参数传递给 tick_params()函数内部。它同时删除 x 轴和 y 轴上的刻度。

Python

# importing matplotlib library
import matplotlib.pyplot as plt
  
# making points to be plotted on x-axis and y-axis
X_axis = [i for i in range (10, 110, 10)]
Y_axis = [2*j+5 for j in range (10, 110, 10)]
  
# printing points to be plotted on the x and y-axis
print("Points on x-axis are: ", X_axis)
print("Points on y-axis are: ", Y_axis)
  
plt.figure(figsize = (8,6))
plt.tick_params(left = False, bottom = False)
plt.plot(X_axis, Y_axis)
plt.scatter(X_axis, Y_axis)
plt.show()

输出:

使用 tick_params() 方法删除 x 和 y 轴上的刻度后绘制的图

案例 3:当我们想从两个轴上同时删除刻度和标签时

要同时从两个轴上删除刻度和标签,除了将leftright属性设置为False 之外,我们还将使用两个额外的标签属性,即labelleftlabelbottom ,我们将其值设置为False并将其传递给内部 tick_params() 函数。它也会从两个轴上删除标签。

蟒蛇3

# importing matplotlib library
import matplotlib.pyplot as plt
  
# making points to be plotted on x-axis and y-axis
X_axis = [i for i in range (10, 110, 10)]
Y_axis = [2*j+5 for j in range (10, 110, 10)]
  
# printing points to be plotted on the x and y-axis
print("Points on x-axis are: ", X_axis)
print("Points on y-axis are: ", Y_axis)
  
plt.figure(figsize = (8,6))
plt.tick_params(left = False, right = False , labelleft = False ,
                labelbottom = False, bottom = False)
  
plt.plot(X_axis, Y_axis)
plt.scatter(X_axis, Y_axis)
plt.show()

输出:

使用 tick_params() 方法删除 x 和 y 轴上的刻度和标签后绘制的图