Printing timestamps (hour / minute / second) using Matplotlib

I want to build some timestamps (Year-month-day Hour-Minute-Second format). I use the following code, however it does not show any information about hour minutes, it shows them as 00-00-00. I double checked my array of dates, and as you can see from the snippet below, they are not zero.

Do you have any idea why I get 00-00-00? enter image description here

import matplotlib.pyplot as plt import matplotlib.dates as md import dateutil dates = [dateutil.parser.parse(s) for s in datestrings] # datestrings = ['2012-02-21 11:28:17.980000', '2012-02-21 12:15:32.453000', '2012-02-21 23:26:23.734000', '2012-02-26 17:42:15.804000'] plt.subplots_adjust(bottom=0.2) plt.xticks( rotation= 80 ) ax=plt.gca() xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S') ax.xaxis.set_major_formatter(xfmt) plt.plot(dates[0:10],plt_data[0:10], "o-") plt.show() 
+6
source share
2 answers

Try to zoom in on the graph, you will see that the dates expand as the x-axis scales.

building unix timestamps in matplotlib

I had a similar annoying problem when trying to build heatmaps of positive selection on chromosomes. If I distorted too much, everything would disappear completely!

edit: this code displays your dates exactly as you give them, but does not add ticks between them.

 import matplotlib.pyplot as plt import matplotlib.dates as md import dateutil datestrings = ['2012-02-21 11:28:17.980000', '2012-02-21 12:15:32.453000', '2012-02-21 23:26:23.734000', '2012-02-26 17:42:15.804000'] dates = [dateutil.parser.parse(s) for s in datestrings] plt_data = range(5,9) plt.subplots_adjust(bottom=0.2) plt.xticks( rotation=25 ) ax=plt.gca() ax.set_xticks(dates) xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S') ax.xaxis.set_major_formatter(xfmt) plt.plot(dates,plt_data, "o-") plt.show() 
+7
source

I can tell you why it shows 00:00:00. This is because it is the start time of this particular day. For example, one tick is located at 2012-02-22 00:00:00 (12 midnight 2012-02-22), and the other at 2012-02-23 00:00:00 (12 midnight 2012-02-23).

Keys for timestamps between these two points are not displayed.

I myself am trying to figure out how to show ticks between these moments.

0
source

Source: https://habr.com/ru/post/949678/


All Articles