Skip to content

Rectangle patch added to a datetime x-axis is plotted with the wrong width #10349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Khris777 opened this issue Jan 30, 2018 · 5 comments
Closed

Comments

@Khris777
Copy link

Khris777 commented Jan 30, 2018

Bug report

Bug summary

When adding a Rectangle patch to a plot with a datetime x-axis the width of the patch is plotted wrong.

Code for reproduction

import pandas as pd
import matplotlib.pyplot as mpp
import matplotlib.patches as patches
import datetime

data = pd.DataFrame({"Timestamp":pd.date_range(start=datetime.datetime(2018,1,30,0,0,0),end=datetime.datetime(2018,2,8,0,0,0),freq='d'),"Requests":[5,4,7,3,5,4,5,6,7,8]})
fig,ax = mpp.subplots()
ax.plot(data.Timestamp,data.Requests)
ax.add_patch(patches.Rectangle((datetime.datetime(2018,2,2,0,0,0),ax.get_ylim()[0]),datetime.datetime(2018,2,2,0,0,0)+datetime.timedelta(days=2),ax.get_ylim()[1]))
fig.show()

Actual outcome
I'm expecting a patch with a width of two days to appear on the plot, instead the patch fills the whole plot to the right rim.

Calling ax.patches[0].get_width() correctly returns datetime.datetime(2018, 2, 4, 0, 0).

It doesn't matter if you use datetime.datetime, np.datetime64, or pd.Timestamp, the results are the same.

EDIT: To get the correct width of two days, width really just has to set to 2.

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 2.1.2
  • Matplotlib backend: Qt5Agg
  • Python version: 3.6.4
  • Other libraries: Pandas 0.22.0, Numpy 1.13.3

Libraries installed with Conda, channels: defaults, conda-forge, anaconda-fusion

@jklymak
Copy link
Member

jklymak commented Jan 30, 2018

  1. Patches are more primitive, and don't get run through the units conversion machinery, so you have to convert by hand using date2num.
  2. https://matplotlib.org/devdocs/api/_as_gen/matplotlib.patches.Rectangle.html Rectangle wants a width and height, not the x1, y1 co-ordinates.

I'm going to close because this isn't a bug.

import matplotlib
matplotlib.use('Qt5Agg')
import pandas as pd
import matplotlib.pyplot as mpp
import matplotlib.patches as patches
import matplotlib.dates as mdates
import datetime
import numpy as np

data=dict()
data['timestamps'] = [datetime.datetime(2018,2,1)+datetime.timedelta(days=x) for x in range(0,10)]

data['Requests'] = np.array([5,4,7,3,5,4,5,6,7,8])

fig,ax = mpp.subplots()
ax.plot(data['timestamps'], data['Requests'])
print(mdates.date2num(datetime.datetime(2018,2,2,0,0,0)))
ax.add_patch(patches.Rectangle(
    (mdates.date2num(datetime.datetime(2018,2,2,0,0,0)), ax.get_ylim()[0]),
    2., ax.get_ylim()[1]))
mpp.show()

@jklymak jklymak closed this as completed Jan 30, 2018
@afvincent
Copy link
Contributor

Well FWIW, the following seems to be working with the master branch (but not with Matplotlib 2.1.2):

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from datetime import datetime, timedelta

data = pd.DataFrame({
            "Timestamp":pd.date_range(start=datetime(2018,1,30,0,0,0),
                                      end=datetime(2018,2,8,0,0,0),freq='d'),
            "Requests":[5,4,7,3,5,4,5,6,7,8]})

fig, ax = plt.subplots()
ax.plot(data.Timestamp, data.Requests)

xy = datetime(2018,2,2,0,0,0), ax.get_ylim()[0]
w, h = timedelta(days=2), ax.get_ylim()[1] - ax.get_ylim()[0]
ax.add_patch(Rectangle(xy, w, h))

fig.show()

(Just changed the Rectangle patch instantiation to use width [but still with timedelta type] and height, instead of [x1, y1] as @jklymak pointed out)

@jklymak
Copy link
Member

jklymak commented Jan 30, 2018

Oh, so it does! I was getting some bizarre error, but its gone now! Good job..

@afvincent
Copy link
Contributor

@jklymak I guess that it does since #9072 (I did not bisect to check though)

@jklymak
Copy link
Member

jklymak commented Jan 30, 2018

Looks correct - I must have had my git repository in some strange state that didn't include that PR...

My workaround works until 2.2 comes out...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants