<p>x轴数据的限制仅从<code>2012-12-05 06:12:06</code>到<code>2012-12-05 08:22:19</code>。你必须扩大日期范围。</p>
<p>但是,您可以使用此代码每3个月设置一次x轴刻度:</p>
<pre><code>import matplotlib.pyplot as plt
from itertools import izip
import datetime
import numpy as np
import pandas as pd
def grouped(iterable, n):
return izip(*[iter(iterable)]*n)
def getLabels(s,t):
labels =[]
for x in pd.date_range(start=s, end=t, freq='3M'):
labels.append(x.strftime("%Y-%m-%d"))
print labels
return labels
arr = [1354648326,1354648326,
1354649456,1371775551,
...
1354655889,1426675579,
1354656139,1420486774]
# convert timestamps to datetime objects
X = list()
Y = list()
for x, y in grouped(arr, 2):
X.append(datetime.datetime.fromtimestamp(x))
Y.append(datetime.datetime.fromtimestamp(y))
# range of X list is only one day: 2012-12-05
# you have to enlarge data of X
print np.min(X),np.max(X)
# sample data
data = np.random.uniform(-10, 10, size=len(X)*len(Y))
# plot
plt.scatter(X, Y, s = data)
ax = plt.gca()
# set limits for X-axis
ax.set_xlim([np.min(X),np.max(X)])
# generate labels
xlabels = getLabels(np.min(X),np.max(X))
# set ticks and labels
ax.set_xticks(xlabels)
ax.set_xticklabels(xlabels,rotation=20)
plt.show()
</code></pre>
<p>如果我扩展x轴限制,我会在您的数据上得到类似的结果:</p>
<pre><code>...
# plot
plt.scatter(X, Y, s = data)
ax = plt.gca()
# set limits for X-axis
xmin = datetime.datetime(2012,1,1,0,0,0) # np.min(X)
xmax = xmin + datetime.timedelta(days = 360) # np.max(X)
ax.set_xlim([xmin, xmax])
# generate labels every 3 month
xlabels = getLabels(xmin, xmax)
# set ticks and labels
ax.set_xticks(xlabels)
ax.set_xticklabels(xlabels,rotation=20)
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/oiPqM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oiPqM.png" alt="enter image description here"/></a></p>
<p>如果您想要更复杂的日期时间刻度标签,请阅读<a href="https://stackoverflow.com/a/38156663/2666859">answer</a>。</p>