0

How can I draw a FancyBboxPatch on a plot with a date x-axis. The FancyBboxPatch should stretch over a specific time period, similar to a gantt chart.

import matplotlib.pyplot as plt
import pandas as pd

data = [('2019-04-15', 'Start'), ('2019-05-01', 'Stop')]
date, labels = zip(*data)
frame = pd.DataFrame({'bla': labels}, index=pd.DatetimeIndex(date))

fig, ax = plt.subplots(figsize=(12.5, 2.5))

for index, row in frame.iterrows():

    ax.text(index, 0.5, row['bla'])

ax.set_xlim([frame.index[0], frame.index[-1]])

plt.show()

enter image description here

I tried to copy and paste and adjust code from the Matplotlib website. However, this caused errors which were related to the date axis and the width of the patch.

Update

I tried with the code below. However, it returns TypeError: unsupported operand type(s) for -: 'Timestamp' and 'float'.

import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.patches as mpatches

data = [('2019-04-15', 'Start'), ('2019-05-01', 'Stop')]
date, labels = zip(*data)
frame = pd.DataFrame({'bla': labels}, index=pd.DatetimeIndex(date))

fig, ax = plt.subplots(figsize=(12.5, 2.5))

for index, row in frame.iterrows():

    ax.text(index, 0.5, row['bla'])

ax.set_xlim([frame.index[0], frame.index[-1]])

ax.add_patch(
    mpatches.FancyBboxPatch(
        (frame.index[0],0.5),  # xy
        100,  # width
        0.5,  # heigth
        boxstyle=mpatches.BoxStyle("Round", pad=0.02)
        )
    )

plt.show()
3
  • 1
    What exactly is the issue here? Commented Nov 15, 2022 at 9:19
  • Sorry, I will elaborate. Commented Nov 15, 2022 at 9:20
  • I suppose there's something wrong with my x,y coordinates? I cannot combine float and a timestamp. However, y is not a time-axis. Commented Nov 15, 2022 at 9:25

1 Answer 1

1

You can try using matplotlib.dates.date2num(d) from here to convert your datetime objects to Matplotlib dates like this:

import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.patches as mpatches
import matplotlib.dates as dates

data = [('2019-04-15', 'Start'), ('2019-05-01', 'Stop')]
date, labels = zip(*data)
frame = pd.DataFrame({'bla': labels}, index=pd.DatetimeIndex(date))

print(f'frame.index[0] {frame.index[0]}')

fig, ax = plt.subplots(figsize=(12.5, 2.5))

for index, row in frame.iterrows():

    ax.text(index, 0.5, row['bla'])

ax.set_xlim([frame.index[0], frame.index[-1]])

ax.add_patch(
    mpatches.FancyBboxPatch(
        (dates.date2num(frame.index[0]),0.5),  # Conversion
        100,  # width
        0.5,  # heigth
        boxstyle=mpatches.BoxStyle("Round", pad=0.02)
        )
    )

plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.