0

let's say I have

df = pd.DataFrame([{'Date': '3/4/2021', 'count': 2}, {'Date': '3/6/2021', 'count': 3}, {'Date': '3/12/2021', 'count': 5}], columns = ['Date', 'count'])

If I graph simply

df.plot(x = 'Date, y='count')

Then the dates on the x axis will be woefully un-sequential and I won't have a useable time horizon on the x axis. How can I make the x axis just a sequential list of dates and then have the plot populated based on whether the value of df['Date'] matches the date on the x axis?

1
  • You will probably have to format the date as time and not as string Commented Apr 23, 2021 at 20:31

1 Answer 1

1

Format the date as date time instead of string.

df.Date = pd.to_datetime(df.Date,format='%d/%m/%Y')
df.plot(x='Date', y='count')

Depending on your localization you will have to change the format to ‘%m/%d/%Y’

enter image description here

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

1 Comment

note that format generally isn't needed. pandas has pretty good auto-detection.

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.