Python 中 x 轴的子图在值之间的跳跃太大

Subplots in Python with x axis having too large of a jump between values

import matplotlib.pyplot as plt
import numpy as np

delta = 0.0001

t = np.arange(0,5+delta,delta)

xt = np.sin(np.pi*t)

fig = plt.figure(1)

ax1= plt.subplot(3,2,1)
ax1.plot(t,xt, "tab:red")
ax1.set(ylabel = "Amplitude")
ax1.set(xlabel = 'Time(s)')
ax1.set(title = 'for n = 1')
ax1.grid()

ax2 = plt.subplot(3,2,2)
ax2.plot(t,xt, "tab:green")
ax2.set(ylabel = "Amplitude")
ax2.set(xlabel = 'Time(s)')
ax2.set(title = 'for n = 2')
ax2.grid()


plt.tight_layout()


plt.show()

您好,这只是我的代码片段,但我的问题基本上与子图的 x 轴有关。 在轴上,值从 0-2-4 跳跃,我需要它从 0-1-2-3-4-5 开始。 有没有办法让这些值显示在 x 轴上,而不仅仅是 0-2-4。

您可以设置x轴的定位器。

import matplotlib as mpl
ax1.xaxis.set_major_locator(mpl.ticker.MultipleLocator(1))
ax2.xaxis.set_major_locator(mpl.ticker.MultipleLocator(1))

有几种可能的方法可以做到这一点。最简单的方法之一是手动设置 x 刻度。

ax1.set_xticks(np.arange(6))
ax2.set_xticks(np.arange(6))