-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Description
Issue setup:
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(123)
df = pd.DataFrame(index=pd.date_range("2017-01-01", "2017-01-07"),
data=np.random.random(size=7))
Problem description
If you plot a Series with a DateTimeIndex of 'daily granularity', and subsequently add other things (e.g. axvline, scatter...) to that plot with a lower level of granularity (e.g. hourly), then the other things you add will be 'snapped' to the nearest day.
If, for example, we try and plot a vertical line at 12pm on the 4th Feb, the line will snap to midnight:
fig, ax = plt.subplots()
df.plot(ax=ax)
ax.axvline(dt.datetime(2017,1,4,12), color ='r')
Reversing the order in which the things are plotted fixes the problem:
ax.axvline(dt.datetime(2017,1,4,12), color ='r')
df.plot(ax=ax)
An alternative fix is to specify the DateTimeIndex at a lower granularity:
df.index = df.index + pd.to_timedelta("1 sec")
So, to wrap up, not a major problem, but certainly something that could catch you unawares... Perhaps this is more an issue on the matplotlib side though?
Output of pd.show_versions()
pandas: 0.19.2
nose: None
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.12.0
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 5.2.2
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 2.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.5.3
html5lib: 0.999
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.4
boto: None
pandas_datareader: None