plt drawing draws major and minor tick marks

This is mainly the division of the primary and secondary scales on the coordinate axis introduced here. Here, two modules in the ticker need to be introduced separately to set MultipleLocator, FormatStrFormatter

  • set_major_locator() set the major scale
  • set_minor_locator() sets the minor scale
  • set_major_formatter() Set the major scale format
  • plt.NullLocator() delete the scale display
  • plt.NullFormatter() removes text formatting
  • tick_params() sets some required parameters

The sub-scale is only the delineation of the range, and does not display the corresponding value or character. Generally, there is no need to set the format of the sub-scale

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

# 数据
x = np.linspace(-10, 10 * np.pi)
y1 = np.sin(x)*10

# 绘图
fig, ax = plt.subplots(figsize=(4,3))
ax.plot(x, y1)

# ----------------------------
# 设置y轴范围
ax.set_ylim(ymin=-10, ymax=10)

# 设置y轴主要刻度线
ymajorLocator = MultipleLocator(5)             # 将y轴主刻度标签设置为,以5为间隔
ymajorFormatter = FormatStrFormatter('%.1f')   # 设置y轴标签文本的格式

ax.yaxis.set_major_locator(ymajorLocator)      # 设置y轴主刻度
ax.yaxis.set_major_formatter(ymajorFormatter)  # 设置y轴标签文本格式

# 设置y轴次要刻度线
yminorLocator = MultipleLocator(1)             # 将此y轴次刻度标签设置为,以1为间隔
ax.yaxis.set_minor_locator(yminorLocator)      # 设置y轴次刻度
# ----------------------------

# ----------------------------
# 设置x轴范围
ax.set_xlim(xmin=-10, xmax=10)

# 设置x轴主要刻度线
xmajorLocator = MultipleLocator(5)            # 将x轴主刻度标签设置为,以5为间隔
xmajorFormatter = FormatStrFormatter('%.1f')  # 设置x轴标签文本的格式

ax.xaxis.set_major_locator(xmajorLocator)     # 设置x轴主刻度
ax.xaxis.set_major_formatter(xmajorFormatter) # 设置x轴标签文本格式

# 设置x轴次要刻度线
xminorLocator = MultipleLocator(1)            # 将此x轴次刻度标签设置为,以1为间隔
ax.xaxis.set_minor_locator(xminorLocator)     # 设置x轴次刻度
# ----------------------------

ax.xaxis.grid(True, which='major', linestyle="--", color="lightgray", linewidth=0.75)     # x坐标轴的网格使用主刻度
ax.yaxis.grid(True, which='minor', linestyle="--", color="lightgray", linewidth=0.75) # y坐标轴的网格使用次刻度
plt.show()

Another example, more concise:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

# 数据
x = np.linspace(-10, 10 * np.pi)
y1 = np.sin(x)*10

# 绘图
fig, ax = plt.subplots(figsize=(4,3))
ax.plot(x, y1)

# ----------------------------
# 设置xy轴范围
plt.axis([-10, 10, -10, 10])
# ----------------------------

# ----------------------------
# 设置y轴主要刻度线
ymajorLocator = MultipleLocator(5)             # 将y轴主刻度标签设置为,以5为间隔
ymajorFormatter = FormatStrFormatter('%.1f')   # 设置y轴标签文本的格式

ax.yaxis.set_major_locator(ymajorLocator)      # 设置y轴主刻度
ax.yaxis.set_major_formatter(ymajorFormatter)  # 设置y轴标签文本格式
# ----------------------------

# ----------------------------
# 设置x轴主要刻度线
xmajorLocator = MultipleLocator(5)            # 将x轴主刻度标签设置为,以5为间隔
xmajorFormatter = FormatStrFormatter('%.1f')  # 设置x轴标签文本的格式

ax.xaxis.set_major_locator(xmajorLocator)     # 设置x轴主刻度
ax.xaxis.set_major_formatter(xmajorFormatter) # 设置x轴标签文本格式
# ----------------------------

# ----------------------------
# 设置主次刻度参数
ax.minorticks_on()
ax.tick_params(axis="both", which="major", direction="out", width=1, length=5)
ax.tick_params(axis="both", which="minor", direction="out", width=1, length=3)
# ax.xaxis.set_minor_locator(MultipleLocator(0.4))
# ----------------------------

# ----------------------------
# 设置网格
plt.grid(True, which="major", linestyle="--", color="gray", linewidth=0.75)
plt.grid(True, which="minor", linestyle=":", color="lightgray", linewidth=0.75)
# ----------------------------

plt.show()

Guess you like

Origin blog.csdn.net/qq_45100200/article/details/131777790