【发布时间】:2020-06-25 11:33:55
【问题描述】:
我想用两个 x 轴绘制一些东西。但是 xticks 不对齐。不知何故,边距被忽略了?!在最终版本中,xlabels 是不同的,这意味着仅在顶部显示轴不是一种选择。
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10.0, 4.0))
axs = fig.subplots(2, 2)
xticklabels = [str(x) for x in range(0, 40+1, 5)]
y = np.random.rand(41*8)
ax0 = axs[0,0].twiny()
axs[0,0].set_xticks(np.arange(0,41*8,5*8))
axs[0,0].set_xticklabels(xticklabels)
ax0.set_xlim(axs[0,0].get_xlim())
ax0.set_xticks(np.arange(0,41*8,5*8))
ax0.set_xticklabels(xticklabels)
axs[0,0].plot(y)
plt.show()
编辑: 其实我想要这样的东西:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10.0, 4.0))
axs = fig.subplots(2, 2)
xticklabels = [str(x) for x in range(0, 40+1, 5)]
y = np.random.rand(41*8)
ax0 = axs[0,0].twiny()
axs[0,0].set_xticks(np.arange(0,41*8,5*8))
axs[0,0].set_xticklabels(xticklabels)
ax0.set_xlim(axs[0,0].get_xlim())
ax0.set_xticks(np.arange(10*8,31*8,5*8))
ax0.set_xticklabels(["0", "25", "50", "75", "100"])
axs[0,0].plot(y)
plt.show()
但是您可以看到刻度不对齐。我快疯了!
【问题讨论】:
标签: python numpy matplotlib