0

I have this simple pandas dataframe with dates as index:

df=pd.DataFrame({'a':[20,30,12],'b':[15,18,18]},index=['2021-10-7','2021-10-8','2021-10-9']) df.index=pd.to_datetime(df.index) when I try to plot a simple pandas.plot with only two dates in xaxis

df.iloc[-2:].plot()

it gives me the following plot with lot of numbers in axis

Plot for only tow dates

Plot works fine if I plot the entire db: db.plot()

Thank you for support

enter image description here

1 Answer 1

1

You can add below line after setting your index to make it work.

df.index.freq = 'D'

So Your entire code looks like this:

df=pd.DataFrame({'a':[20,30,12],'b':[15,18,18]},index=['2021-10-7','2021-10-8','2021-10-9']) 

df.index  = pd.to_datetime(df.index)

df.index.freq = 'D'

Alternatively:

You can also use date_range like below : Please note this would work only if your data is like the one provided which has frequency of daily, You need to adjust in cases where the frequencies are different.

df=pd.DataFrame({'a':[20,30,12],'b':[15,18,18]},index=['2021-10-7','2021-10-8','2021-10-9']) 

df.index=pd.date_range(start = '2021-10-07', end='2021-10-09')

Both approaches will give you same plot which you have mentioned in the question(similar to bottom one in the provided question)

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.