I tried to plot a rectangle on a graph with a datetime x-axis using the following code:
from datetime import datetime, timedelta
from matplotlib.patches import Rectangle
import matplotlib.pyplot as plt
# Create new plot
fig = plt.figure()
ax = fig.add_subplot(111)
# Create rectangle
startTime = datetime.now()
width = timedelta(seconds = 1)
endTime = startTime + width
rect = Rectangle((startTime, 0), width, 1, color='yellow')
# Plot rectangle
ax.add_patch(rect) ### ERROR HERE!!! ###
plt.xlim([startTime, endTime])
plt.ylim([0, 1])
plt.show()
However, I get the error:
TypeError: unsupported operand type(s) for +: 'float' and 'datetime.timedelta'
What's going wrong?
(I'm using matplotlib version 1.0.1)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…