简体   繁体   中英

Spectrogram date changed to 1970 when I use set_major_formatter and locator

I was trying to plot spectrogram by using 'plt.specgram'.

The date was from 2005-2007 but it moved to 1970.

    df=pd.read_csv('Data.csv')
    df.Date=pd.to_datetime(df.Date)
    df=df.set_index('Date')
    plt.figure(figsize=(20,10))
    plt.specgram(df, Fs=1)
    plt.gca().xaxis.set_major_formmater(mdates.DateFormatter('%Y-%m')
    plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=3))

(Please don't care about the color but date)

在此处输入图像描述

In this case, what I have to do to use right date range?

eg

Date Value
2005-05-01 0.52264
2005-05-02 0.45656
... ...
2007-02-27 0.558954
2007-02-28 0.679884

Use reset_index() before setting the date index.

df=pd.read_csv('Data.csv')
df.Date=pd.to_datetime(df.Date)
df=df.reset_index()
df=df.set_index('Date')
plt.figure(figsize=(20,10))
plt.specgram(df, Fs=1)
plt.gca().xaxis.set_major_formmater(mdates.DateFormatter('%Y-%m')
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=3))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM