为什么在运行此代码时pyplot会随机加入点?
def plot_date_value(single_data_frame):
date_axis_data = []
value_axis_data = []
date_ticks = []
for item in single_data_frame:
date_axis_data.append(date2num(item[0]))
date_ticks.append(item[0])
value_axis_data.append(item[1])
print(item[0], '---------------------', item[1])
fig = plt.figure()
graph = fig.add_subplot(111)
graph.plot(date_axis_data, value_axis_data, 'r-o')
graph.set_xticks(date_axis_data)
graph.set_xticklabels([date.strftime("%Y-%m-%d %H:%M") for date in date_ticks])
plt.show()
我对此功能的数据是:
[(datetime.datetime(2011, 9, 14, 6, 0), 0.83697607), (datetime.datetime(2011, 9, 14, 8, 0), 1.010857357), (datetime.datetime(2011, 9, 14, 10, 0), 0.982353533), (datetime.datetime(2011, 9, 14, 16, 0), 0.962431422), (datetime.datetime(2011, 9, 15, 20, 0), 0.971906937), (datetime.datetime(2011, 9, 16, 2, 0), 1.000917626), (datetime.datetime(2011, 9, 17, 2, 0), 0.827756728), (datetime.datetime(2011, 9, 14, 18, 0), 0.898688627), (datetime.datetime(2011, 9, 14, 20, 0), 0.978427012), (datetime.datetime(2011, 9, 15, 18, 0), 0.822463165), (datetime.datetime(2011, 9, 16, 16, 0), 1.222488219), (datetime.datetime(2011, 9, 16, 20, 0), 0.909770116), (datetime.datetime(2011, 9, 16, 22, 0), 1.121605357)]
它吐出的图是:
为什么会这样?任何帮助将不胜感激。
答案 0 :(得分:4)
按照给定的顺序连接点。您的日期未分类。
尝试将for
循环更改为:
for item in sorted(single_data_frame):