<p>问题是图形只有五个刻度,因此只能显示五个标签。如果要显示所有标签,则需要确保刻度数相同。</p>
<p>我没有安装pandas,也没有完整的数据,所以无法重新创建标签。我只是复制了你提供的标签清单。我还从标签中“逆向设计”了x轴的最小值和最大值(以便数据绘制在正确的位置)。</p>
<p>这一行:<code>ax.xaxis.set_ticks(np.arange(min_x, max_x, int((max_x-min_x)/len(labels))))</code>
确保刻度数与标签数相同。</p>
<p>请注意,我还更改了标签的水平对齐方式,以便即使压扁了标签,也仍然可以清楚地看到标签对应的刻度。这段数据似乎是在正确的位置绘制的,所以我很确定标签是在正确的位置。</p>
<p>(显然y轴可以用同样的方法处理)</p>
<pre><code>import matplotlib.pyplot as plt
import numpy as np
import time
import datetime
labels =['2012-06-30', '2012-09-30', '2012-12-31', '2013-03-31',
'2013-06-30', '2013-09-30', '2013-12-31', '2014-03-31',
'2014-06-30', '2014-09-30', '2014-12-31', '2015-03-31',
'2015-06-30', '2015-09-30', '2015-12-31', '2016-03-31']
x = []
y = []
with open('data.txt','r') as myfile:
for line in myfile:
_x, _y = line.strip().split(',')
x.append(int(_x))
y.append(int(_y))
min_x = int(time.mktime(datetime.datetime.strptime('2012-06-30','%Y-%m-%d').timetuple()))
max_x = int(time.mktime(datetime.datetime.strptime('2016-03-31','%Y-%m-%d').timetuple()))
print (datetime.datetime.fromtimestamp(min(x)).strftime('%Y-%m-%d'))
# Confirm that we are plotting in the right place for this sample
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_xlim(min_x, max_x)
ax.xaxis.set_ticks(np.arange(min_x, max_x, int((max_x-min_x)/len(labels))))
ax.set_xticklabels(labels, rotation=20, horizontalalignment = 'right')
ax.scatter(x,y)
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/zvJtt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zvJtt.png" alt="enter image description here"/></a></p>