1

I am trying to plot time series data, and label the x-axis by month. This is the first few of my dataframe:

    Time                Value
2012-01-01 00:00:00     1.223
2012-01-01 00:00:30     2.132
2012-01-01 00:01:00     1.417
2012-01-01 00:01:30     1.767                     

And my code is as follows:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import MonthLocator
%matplotlib inline

df = pd.read_csv('data.csv', index_col=0)
value = df.value
plt.figure(figsize=(15,10))
ax = value.plot(x_compat=True)
ax.xaxis.set_major_locator(MonthLocator(bymonthday=15))
ax.xaxis.set_major_formatter(plt.NullFormatter())
plt.show()

As suggested in other answers, I already added the (x_compat=True) argument. However, this doesn't give the ticks and ticklabels of my x-axis at all. How can I fix this?

And I also tried the second way with matplotlib by this

plt.figure(figsize=(15,10))
plt.plot(df.index, df.value)
plt.gca().xaxis.set_major_locator(MonthLocator(bymonthday=15))
plt.gca().xaxis.set_major_formatter(DateFormatter("%M"))

But still I can't see the ticklabels in month. It doesn't give me anything on x-axis.

Answer I should convert df.index using to_datatime first.

user123
  • 231
  • 2
  • 12

0 Answers0