简体   繁体   中英

Plot Datetime x-axis evenly spaced days

I have several datetime objects of measurements taken during several points in the day. I have several days worth of these measurements but the days on which they were taken are quite random. However, I would like to be able to do a matplotlib plot where the x-axis shows the month and day but have the measurements, several of which are recorded during the day, be evenly spaced. So far, I've only been able to generate a plot where the days of spaced in accordance with the time that passed, but I want them evenly spaced. Are there any examples online or a quick solution that might help?

My data looks like this:

10-01-2003   3
10-01-2003   4
10-02-2003   3
10-10-2003   5
01-02-2004   4

where I would want the labels to be %m-%d and have each measurement be evenly spaced.

You can try generating an array of evenly spaced values and replace the ticks with the dates (I left the year on the plots but they could easily be removed like you mentioned):

data = np.array([3,4,3,5,4])
dummy_values = np.arange(0,5,1)
times = np.array(['10-01-2003','10-01-2003','10-02-2003','10-10-2003','01-02-2004'])

plt.figure()
plt.plot(dummy_values,data)
plt.xticks(dummy_values,times)

Which will give you a plot that looks like this: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM