本文介绍了为什么 pyplot(matplotlib) 随机连接点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么我运行这段代码时pyplot随机连接点?
Why is pyplot joining points randomly when I run this code?
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()
我给这个函数的数据是这样的:
My data to this function is this:
[(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)]
它吐出的图形是这样的:
The graph it spits out is this:
为什么会这样?任何帮助将不胜感激.
Why is this happening? Any help will be greatly appreciated.
推荐答案
它以给定的顺序连接点.您的日期未排序.
It connects the points in the order given. Your dates are not sorted.
尝试将 for
循环更改为:
Try changing the for
loop to:
for item in sorted(single_data_frame):
这篇关于为什么 pyplot(matplotlib) 随机连接点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!