0

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).

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

Matplotlib improperly plotted chart

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?

Thanks to all python gurus for your assistance!

1 Answer 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".

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: enter image description here

pltrdy


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

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, thank you! Since I'm obtaining these two lists from a dictionary, do you think it will be faster to get the data as [(time, value), (time, value)] and then sort the list, before plotting, versus zipping?
Not sure to understand your point. If you don't have hugely huge data this may be ok anyway. I usually used library like Pandas or numpy so I don't really what is the best when built in function are involved (because they are not, usually, the best if performance matters)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.