matplotlib-保持记号位置均匀,但值不均匀

matplotlib-保持记号位置均匀,但值不均匀,matplotlib,Matplotlib,matplotlib中是否有任何方法可以保持勾号位置均匀,而保持其值不均匀,以便数据可以压缩某个时间间隔并在另一个时间间隔扩展。 例如,以下代码生成带刻度的正弦波[0.0,0.5,1.0,1.5,2.0] import matplotlib.pyplot as plt import numpy as np t = np.arange(0.0, 2.0, 0.01) s = 1 + np.sin(2*np.pi*t) plt.plot(t, s) plt.xlabel('time (s)') pl

matplotlib中是否有任何方法可以保持勾号位置均匀,而保持其值不均匀,以便数据可以压缩某个时间间隔并在另一个时间间隔扩展。 例如,以下代码生成带刻度的正弦波[0.0,0.5,1.0,1.5,2.0]

import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
ax = plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.autoscale(False)
ax.xaxis.set_ticks([0.0,0.5,1.0,1.5,2.0])
plt.show()
我想将ax.xaxis.set_ticks([0.0,0.5,1.0,1.5,2.0])处的值0.5更改为0.25,但将其保持在绘图上的同一位置。

显然,以下不是OP要求的。我将把它留在这里,直到问题被编辑,这样人们至少能理解什么是不需要的

您可以添加
set_ticklabels
以不同方式标记记号

ax.xaxis.set_ticks(     [0.0, 0.50, 1.0,1.5,2.0])
ax.xaxis.set_ticklabels([0.0, 0.25, 1.0,1.5,2.0])
Comlpete示例:

import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
ax = plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.autoscale(False)
ax.xaxis.set_ticks([0.0,0.5,1.0,1.5,2.0])
ax.xaxis.set_ticklabels([0.0,0.25,1.0,1.5,2.0])
plt.show()

我也在做类似的事情

我认为你想做的是:

ax.set_xticks((0,0.25,1,1.5,2)) # makes ticks values uneven
ax.xaxis.set_minor_locator(plt.MultipleLocator(0.25)) # locates ticks at a multiple of the number you provide, as here 0.25 (keeps ticks evenly spaced)

我的意图不是改变标签,而是仅仅改变勾号的值,并期望根据勾号值绘制数据。这还不完全清楚。例如,0.4或0.6应位于何处?你可能想在问题中更详细地解释你想要实现的目标。实际上,我想要实现的是,当我更改勾号值时,我希望matplotlib不要更改勾号的位置,而不是改变它绘制数据的方式。例如,对于正弦波,我希望将值0.5更改为0.25,并希望保持该值代替0.5。通过这样做,我希望matplotlib能够根据滴答声间隔(而不是自动间隔)绘制正弦波。正如我所说的,由于我不明白您当时想要什么,您需要编辑您的问题以更好地解释这一点。也请注意问题下面的评论。你至少需要提供一个手绘的模型来展示你想要实现的目标。正如下面的答案所表明的,你想要什么完全不清楚。这就是你想要的吗?在x轴的1/4上从0-0.25绘制,在剩余的3/4上从0.25-2.0绘制?