Timeline of day versus date in matplotlib

I want, for some reason, to plot on the x-axis and time of day on the y-axis, and then have either line charts or intervals (floating bars).

This SO answer will help

But I have a few differences from this plot, and I can’t get it to work. I actually get the y axis for plotting both DATES and time, so it picks up monthly timestamps on the y axis when I just need about one day. This example states in the comments: “the base date for yaxis can be anything, because the information is at that time,” but I don’t understand how it “discards” the information about the base date.

Anyway, my needs are:

  • I need an option for a 24-hour time (from 00:00 to 24:00) or am / pm style time for the y axis, while the axis ticks look like 15:00, 11:00, etc. This will be done using FuncFormatter, I think.

  • For intervals (time intervals) I do not want to use error lines - the lines are too thin. I would like to use a (floating) bar / column chart.

My data is a date and time string in the format '2010-12-20 05:00:00'

Thanks for any help.

+4
source share
2 answers

I think you are a little confused about how matplotlib handles time and dates backstage.

All dates in matplotlib are presented as simple floats. 1 day corresponds to a difference of 1.0, and dates - in days since 1900 (if I remember correctly).

So, in order to draw only the time of a given date, you need to use % 1 .

I'm going to use glasses, but you can easily use bars. Look at using the bottom keyword argument if you use plt.bar so that the bottom of the bars starts at the start of your intervals (and remember that the second argument is the height of the bar and not its top is the Y value).

For instance:

 import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np import datetime as dt # Make a series of events 1 day apart x = mpl.dates.drange(dt.datetime(2009,10,1), dt.datetime(2010,1,15), dt.timedelta(days=1)) # Vary the datetimes so that they occur at random times # Remember, 1.0 is equivalent to 1 day in this case... x += np.random.random(x.size) # We can extract the time by using a modulo 1, and adding an arbitrary base date times = x % 1 + int(x[0]) # (The int is so the y-axis starts at midnight...) # I'm just plotting points here, but you could just as easily use a bar. fig = plt.figure() ax = fig.add_subplot(111) ax.plot_date(x, times, 'ro') ax.yaxis_date() fig.autofmt_xdate() plt.show() 

Time vs. Date

Hope this helps a bit!

+6
source

Follow Joe Kingston's answer, but I would add that you need to convert date strings to datetime objects, which can easily be done using the datetime module, and then convert them to matplotlib dates, represented as float (which can then be formatted as desired) .

 import datetime as dt import matplotlib as mpl some_time_str = '2010-12-20 05:00:00' some_time_dt = dt.datetime.strptime(some_time_str, '%Y-%m-%d %H:%M:%S') some_time_num = mpl.dates.date2num(some_time_dt) # 734126.20833333337 

If you start with an array filled with temporary lines, you can do something like:

 time_str_list = ['2010-12-20 05:00:00','2010-12-20 05:30:00'] time_num_list = map( lambda x: mpl.dates.date2num(dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')), time_str_list) #[734126.20833333337, 734126.22916666663] 
+1
source

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


All Articles