plot x-axis as date in matplotlib

后端 未结 2 1267
傲寒
傲寒 2020-12-02 02:33

I am trying to perform some analysis on data. I got csv file and I convert it into pandas dataframe. the data looks like this. Its has several columns, but I am trying to dr

相关标签:
2条回答
  • 2020-12-02 02:47

    Set the index to the datetime series

    If you set the index to the datetime series matplotlib will handle the x axis for you. Here is a minimal example of how you might deal with this visualization.

    Simple example:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    date_time = ["2011-09-01", "2011-08-01", "2011-07-01", "2011-06-01", "2011-05-01"]
    date_time = pd.to_datetime(date_time)
    temp = [2, 4, 6, 4, 6]
    
    DF = pd.DataFrame()
    DF['temp'] = temp
    DF = DF.set_index(date_time)
    
    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom=0.3)
    plt.xticks(rotation=90)
    plt.plot(DF)
    

    This will yield a plot that looks like the following:

    Setting the index makes things easier

    The important note is that setting the DataFrame index to the datetime series allows matplotlib to deal with x axis on time series data without much help.

    Follow this link for detailed explanation on spacing axis ticks (specifically dates)

    0 讨论(0)
  • 2020-12-02 03:02

    You missed a ' line 12. It cause the SyntaxError.

    This should correct the error.

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import datetime
    import matplotlib.dates as mdates
    
    df = pd.read_csv('rio_data.csv', delimiter=',')
    print (df.head(10))
    d = []
    for dat in df.date:
        # print (dat)
        d.append(datetime.strptime(df['date'], '%Y-%m-%d'))
    days = dates.DayLocator()
    datemin = datetime(2011, 1, 1)
    datemax = datetime(2012, 4, 20) 
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.xaxis.set_major_locator(days)
    ax.set_xlim(datemin, datemax)
    ax.set_ylabel('Count values') 
    
    0 讨论(0)
提交回复
热议问题