擅长:python、mysql、java
<p>在数据坐标中设置ticklabel位置是相当不寻常的。但还是有可能的。与<a href="https://stackoverflow.com/questions/48326151/moving-matplotlib-xticklabels-by-pixel-value">this question</a>类似,您可以使用转换来移动标签。在这里,我们首先将它们移动1个数据单位,然后应用通常的变换。在</p>
<pre><code>import matplotlib.pyplot as plt
import numpy as np; np.random.seed(42)
import matplotlib.transforms as mtrans
x = np.linspace(2013.6,2018.4)
y = np.cumsum(np.random.randn(50))/12.+0.3
ax = plt.subplot(111)
ax.yaxis.tick_right()
ax.spines['bottom'].set_position('zero')
ax.spines['bottom'].set_color('xkcd:light grey')
ax.set_facecolor('xkcd:dark grey')
ax.tick_params(axis='x', direction='out', labelbottom=True, pad=0,
color='xkcd:light grey', labelcolor='xkcd:light grey')
ax.plot(x,y)
ax.set_ylim(-1.1,1.1)
trans = mtrans.Affine2D().translate(0,-1.)
for t in ax.get_xticklabels():
t.set_transform(trans + t.get_transform())
plt.tight_layout()
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/84Sg2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/84Sg2.png" alt="enter image description here"/></a></p>