如何关闭 matlibplot 轴的刻度和标记?

要关闭 matplotlib 轴的刻度和标记,我们可以采取以下步骤 -

  • 设置图形大小并调整子图之间和周围的填充。

  • 使用 numpy 创建 x 和 y 数据点。

  • 使用方法绘制 x 和 y 数据点plot()

  • 获取绘图的当前轴。

  • 用于set_tick_params()隐藏 X 和 Y 轴标签标记。

  • 使用set_xticks()set_yticks()隐藏 X 和 Y 轴刻度线。

  • 要显示图形,请使用show()方法。

示例

import numpy as np
from matplotlib import pyplot as plt

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create x and y data points
x = np.linspace(-10, 10, 100)
y = np.sin(x)

plt.plot(x, y)
ax = plt.gca()

# Hide X and Y axes label marks
ax.xaxis.set_tick_params(labelbottom=False)
ax.yaxis.set_tick_params(labelleft=False)

# Hide X and Y axes tick marks
ax.set_xticks([])
ax.set_yticks([])

plt.show()
输出结果

它将产生以下输出