PLT 図面は主目盛と副目盛を描画します

ここで紹介するのは主に座標軸上の主スケールと副スケールの分割ですが、ここではMultipleLocator、FormatStrFormatterを設定するためにティッカー内の2つのモジュールを別途導入する必要があります

  • set_major_locator() メジャースケールを設定します
  • set_minor_locator() はマイナースケールを設定します
  • set_major_formatter() メジャースケール形式を設定します。
  • plt.NullLocator() スケール表示を削除します
  • plt.NullFormatter() はテキストの書式設定を削除します
  • tic_params() はいくつかの必須パラメータを設定します

サブスケールは範囲を示すだけであり、対応する値や文字は表示されません。通常、サブスケールの形式を設定する必要はありません。

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()

より簡潔な別の例:

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()

おすすめ

転載: blog.csdn.net/qq_45100200/article/details/131777790