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:
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?
Using the DateFormatter module from matplotlib, you can specify the format that you want to use for the date using the syntax: "%X %X" where each %X element represents a part of the date as follows: %Y - 4 digit year with upper case Y. %y - 2 digit year with lower case y. %m - month as a number with lower case m.
MatPlotLib with Python Create a figure and a set of subplots using subplots() method. Plot the dataframe using plot method, with df's (Step 1) time and speed. To edit the date formatting from %d-%m-%d to %d:%m%d, we can use set_major_formatter() method. Set the formatter of the major ticker.
Show() would help whenever there is no interactive plot. fig. Show() would help to display all the figures if it is interactive. Let's take an example to observe the difference between plt.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With