6

I have a simple time series plot in Pandas, that can be emulated by the following code below:

import pandas as pd
import numpy as np
from datetime import datetime, timedelta, date
import time
import matplotlib.pyplot as plt

df2 = pd.DataFrame({'A' : np.random.rand(1440).cumsum()}, index = pd.date_range('1/1/2015', periods=1440, freq='1min'))

df2.A.plot()

Which generates the following graph: enter image description here

My problem is that the date displayed is not relevant to the graph itself and I wish to remove it and leave only the time series on the x axis.

How do I do this?

1 Answer 1

7

you can use matplotlib.dates and its HourLocator to set the date/time formatting:

import pandas as pd
import numpy as np
from datetime import datetime, timedelta, date
import time
import matplotlib.pyplot as plt
import matplotlib.dates as dates

df2 = pd.DataFrame({'A' : np.random.rand(1440).cumsum()}, index = pd.date_range('1/1/2015', periods=1440, freq='1min'))

df2.A.plot()
plt.gca().xaxis.set_major_locator(dates.HourLocator())
plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%H:%M'))

plt.show()

enter image description here

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

1 Comment

Just the HourLocator worked for me, the formatter gave an error.

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.