0

I have minutely data in a dataframe. I'm trying to plot on y axis column C, against the index column date. How can I show 5 dates as x-axis labels?

Currently, I try to plot like this:

df['C'].plot(figsize=(16,6))

However, I can't see any x-axis labels.

df=pd.DataFrame([["2020-07-01 00:00:00",2],["2020-07-01 00:01:00",3]],columns=['date','C'])
thanasisp
  • 5,855
  • 3
  • 14
  • 31
Jonathan
  • 33
  • 4

1 Answers1

0

Here is the solution :

  • Read the dataframe :
df=pd.DataFrame([["2020-07-01 00:00:00",2],["2020-07-01 00:01:00",3]],columns=['date','C'])
  • Change the dtype of date to pandas datetime64[ns].
df.date = pd.to_datetime(df.date)
  • Plot with supplying date as x :
df.plot(x='date')

Output :

enter image description here

You can change the frequency of the ticks using mdates.DateFormatter as explained in the answer here:

## borrowed from https://stackoverflow.com/questions/14946371/editing-the-date-formatting-of-x-axis-tick-labels-in-matplotlib
import matplotlib.dates as mdates
myFmt = mdates.DateFormatter('%d') ## change '%d' depending on your requirement
ax.xaxis.set_major_formatter(myFmt)
Grayrigel
  • 3,474
  • 5
  • 14
  • 32