0

I want to create the following image, where the x-axis contains blocks of 1 month each. If the users monthly value is 1, the block should show green, if NaN (or 0) it will be red (or preferably blank). How can I achieve this with matplotlib, or any other package that could achieve this?

Image: enter image description here

Data:

Date     chris.germany     carol.clair  ...  dana.davis
Jan-01         1               NaN                1
Feb-01         1               NaN               NaN
Mar-01         1                1                NaN
Apr-01         1                1                 1
May-01         1                1                NaN
...
Dec-02         1               NaN               NaN
Laurie
  • 1,189
  • 1
  • 12
  • 28
  • 1
    You may look into [this question](https://stackoverflow.com/questions/51505291/timeline-bar-graph-using-python-and-matplotlib/51506028#51506028). – ImportanceOfBeingErnest Jul 26 '18 at 14:05
  • Thankyou, this is exactly what I want. If you could provide a hint as how I can adjust that template to the format of my data, I'd really appreciate it. – Laurie Jul 26 '18 at 14:12

1 Answers1

0

Solution provided by ImportanceofBeingErnest:

This link provides a basic broken_bahr plot through matplotlib. I'm not sure how this can be configured to work with a df, so I inputted the data manually.

Finished picture:

enter image description here

Final code:

import matplotlib.pyplot as plt

col = 'royalblue'

fig, ax = plt.subplots()
ax.broken_barh([(1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), 
            (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), 
            (15, 1), (16, 1), (17, 1), (18, 1), (19, 1), (20, 1), (21, 1), 
            (22, 1), (23, 1), (24, 1)], (2.5, 1), facecolors=col)

ax.broken_barh([(4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), 
            (11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 1), 
            (17, 1), (18, 1), (19, 1), (20, 1), (21, 1), (22, 1), 
            (23, 1), (24, 1)], (4.5, 1), facecolors=col)

ax.broken_barh([(5, 1), (9, 1), (11, 1), (12, 1), (18, 1), (19, 1), (20, 1), (23, 1)], (6.5, 1), facecolors=col)

ax.broken_barh([(2, 1), (8, 1), (18, 1), (20, 1)], (8.5, 1), facecolors=col)

ax.broken_barh([(7, 1), (13, 1), (18, 1), (19, 1), (21, 1)], (10.5, 1), facecolors=col)

ax.set_ylim(1, 13)
ax.set_xlim(0, 24)
ax.set_yticks([3, 5, 7, 9, 11])
ax.set_yticklabels(['chris.germany\n(Trader)', 'carol.clair\n(Lawyer)', 'dana.davis\n(VP)', 'andrew.lewis\n(Director)', 'chris.mallory\n(Analyst)'])
ax.set_xticklabels(['Jan-01', 'Jul-01', 'Dec-01', 'Jun-02', 'Nov-02'])
ax.grid(True)

plt.show()
Laurie
  • 1,189
  • 1
  • 12
  • 28