2

I have created a matplotlib plot based on a pandas dataframe that plots correctly. I am in the process of including vertical lines on the chart. I am able to do that by using this:

permitdate = 'June 15, 2017'

ax.axvline(permitdate, linewidth=2)

Permit date can be any arbitrary date that I want to plot.

The issue I have is that the index (i.e. the x-asix on the chart) is on a month end basis:

DatetimeIndex(['2017-04-30', '2017-05-31', '2017-06-30', '2017-07-31',
               '2017-08-31', '2017-09-30', '2017-10-31', '2017-11-30',
               '2017-12-31'],
              dtype='datetime64[ns]', name='Date', freq=None)

When I try to chart the vlines, it puts the line directly on the month in which the date is.

for example, the permit date from the above shows on top of june x-tick on the a-axis. Is there a way to show it in between the June and July x-ticks that represents it actual place between the months and not directly over the month x axis spot?

Edit:

An example inspired by the pandas docs:

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np

ts = pd.Series(np.random.randn(10), index=pd.date_range('1/1/2000', periods=10, freq='m'))
ts = ts.cumsum()


df = pd.DataFrame(np.random.randn(10, 4), index=ts.index, columns=list('ABCD'))


df = df.cumsum()

ax = df.plot()
designdate = 'July 15, 2000'
ax.axvline(designdate, linewidth=2)

Thanks,

Ivan

2
  • 3
    As always chances are higher to get a satisfactory answer when providing a minimal reproducible example of the issue. Commented Apr 26, 2017 at 18:30
  • @ImportanceOfBeingErnest, added. Thanks for the reminder. Commented Apr 26, 2017 at 19:36

1 Answer 1

1

Use resample to get daily frequency in conjunction with interpolate

ax = df.resample('D').interpolate().plot()
designdate = 'July 15, 2000'
ax.axvline(pd.to_datetime(designdate), linewidth=2)

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

I came across and issue I am unclear on. The above worked for the vlines issue, but the xaxis is not shifted. Using the text example, without the resample the dates do from January to October. All showing the month end dates (and the October 31st being at the rightmost). With the resample, the January ddrops off, and the October is left shifted. I cannot figure out how to put this back to how it was. Thoughts please?
@ivan7707 that's because we interpolate from the end of January to the end of October, daily. The end of Jan looks like Feb. The chart has 1 day of Jan. As for Oct, it has 31 days of Oct and the label starts on the 1st of Oct. You wanted the vline to show up between Jul and Aug. That implies you wanted Jul label on the first of Jul. Well this is the consequence of that.
understood. Thanks.

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.