Matplotlib从两个x和y点列表中错误地绘制数据

时间:2021-07-24 23:42:45

These are the two lists:

这是两个列表:

x = [736297.48567129625, 736297.53253472224, 736297.57858796301, 736297.50894675928, 736297.55557870376]

y = [-757, -706, 385, -247, 126]

Here's my code:

这是我的代码:

import matplotlib.pyplot as plt
import matplotlib.dates as mdate

x = [736297.48567129625, 736297.53253472224, 736297.57858796301, 736297.50894675928, 736297.55557870376]
y = [-757, -706, 385, -247, 126]

plt.figure()
plt.plot_date(x, y, fmt='b-')
plt.grid(True)
plt.show()

The x list values are converted from epoch time, using mdate.epoch2num(epoch).

使用mdate.epoch2num(epoch)从纪元时间转换x列表值。

This is the graph I get, which is obviously not plotted chronologically:

这是我得到的图表,显然没有按时间顺序绘制:

Matplotlib improperly plotted chart

Matplotlib不正确地绘制图表

Bonus question: You can see that the time format on the x axis looks horrible. Any idea how to present it in the format yy-mm-dd-hh-ss?

奖金问题:您可以看到x轴上的时间格式看起来很糟糕。知道如何以yy-mm-dd-hh-ss格式呈现它吗?

Thanks to all python gurus for your assistance!

感谢所有python大师的帮助!

1 个解决方案

#1


1  

pyplot is just plotting in the order you provided your data. It turns out that x isn't sorted so the plot kinda goes "backward".

pyplot只是按照您提供数据的顺序绘图。事实证明,x没有排序,所以情节有点“向后”。

You should then sort you indices (and its corresponding values) to get what you want:

然后,您应该对索引(及其相应的值)进行排序以获得您想要的内容:

#Sort by date (x) and apply change to values (y) 
xx, yy = zip(*sorted(zip(x, y)))
plt.plot_date(xx, yy, fmt='b-')

Giving you the following plot: Matplotlib从两个x和y点列表中错误地绘制数据

给你以下情节:

pltrdy

pltrdy


Bonus question: Have look here for pyplot dates tick formatting. It may be easy to understand and do what you want.

奖金问题:请看这里的pyplot日期刻度格式。它可能很容易理解并做你想做的事情。

#1


1  

pyplot is just plotting in the order you provided your data. It turns out that x isn't sorted so the plot kinda goes "backward".

pyplot只是按照您提供数据的顺序绘图。事实证明,x没有排序,所以情节有点“向后”。

You should then sort you indices (and its corresponding values) to get what you want:

然后,您应该对索引(及其相应的值)进行排序以获得您想要的内容:

#Sort by date (x) and apply change to values (y) 
xx, yy = zip(*sorted(zip(x, y)))
plt.plot_date(xx, yy, fmt='b-')

Giving you the following plot: Matplotlib从两个x和y点列表中错误地绘制数据

给你以下情节:

pltrdy

pltrdy


Bonus question: Have look here for pyplot dates tick formatting. It may be easy to understand and do what you want.

奖金问题:请看这里的pyplot日期刻度格式。它可能很容易理解并做你想做的事情。