如何从 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()
输出:
Points on x-axis are: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Points on y-axis are: [25, 45, 65, 85, 105, 125, 145, 165, 185, 205]
通过查看上图,我们可以观察到默认情况下 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()
输出:
案例 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()
输出:
案例 2:使用 tick_params() 方法在两个轴上删除这些刻度:
要同时删除 x 轴和 y 轴上的刻度,我们可以同时传递left和right属性,将其值设置为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()
输出:
案例 3:当我们想从两个轴上同时删除刻度和标签时
要同时从两个轴上删除刻度和标签,除了将left和right属性设置为False 之外,我们还将使用两个额外的标签属性,即labelleft和labelbottom ,我们将其值设置为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()
输出: