Floating chart

I am trying to make a graph where the x axis is time and the y axis is a histogram that will have bands spanning a certain time period as follows:

                         ______________
                         |_____________|

    _____________________
    |___________________|
----------------------------------------------------->
             time

I have 2 lists of date and time values ​​for the beginning and end of these times that I would like to cover. Still i

x = np.array([dt.datetime(2010, 1, 8, i,0) for i in range(24)])

to cover a 24 hour period. My question is how to set and draw my y values ​​this way?

+4
source share
1 answer

You can use plt.barh:

import datetime as DT
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

start = [DT.datetime(2000,1,1)+DT.timedelta(days=i) for i in (2,0,3)]
end = [s+DT.timedelta(days=i) for s,i in zip(start, [15,7,10])]
start = mdates.date2num(start)
end = mdates.date2num(end)
yval = [1,2,3]
width = end-start

fig, ax = plt.subplots()
ax.barh(bottom=yval, width=width, left=start, height=0.3)
xfmt = mdates.DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_formatter(xfmt)
# autorotate the dates
fig.autofmt_xdate()
plt.show()

gives

enter image description here

+1
source

All Articles