Search code examples
pythonpandasdatehorizontal-line

Make horizontal lines with pandas yearly data?


I have a pandas object with yearly averages data in this form:

DatetimeIndex(['2005-12-31', '2006-12-31', '2007-12-31', '2008-12-31',
               '2009-12-31', '2010-12-31', '2011-12-31'],
              dtype='datetime64[ns]', freq='A-DEC')
2005-12-31    3.347463
2006-12-31    3.042220
2007-12-31    3.296574
2008-12-31    3.082333
2009-12-31    2.471380
2010-12-31    2.337974
2011-12-31    2.083004

I would like to draw horizontal lines from beginning of the year till the end of the year with the values currently associated to the last day of the year. Currently, when I plot this pandas object, I get linear interpolation between the points at the end of the year. I have tried adding indexes with:

new_index= ['2005', '2006', '2007', '2008','2009', '2010', '2011']
df_year.reindex(new_index)

which result in the same graph. Or adding the first day of each year (not good for automation though) with:

z=datetime.strptime('01-01-2005', '%d-%m-%Y')
indx.append(pd.Index([z]))
df_year.set_value(z,2)

which result in:

DatetimeIndex(['2005-12-31', '2006-12-31', '2007-12-31', '2008-12-31',
               '2009-12-31', '2010-12-31', '2011-12-31', '2005-01-01'],
              dtype='datetime64[ns]', freq=None)
2005-12-31    3.347463
2006-12-31    3.042220
2007-12-31    3.296574
2008-12-31    3.082333
2009-12-31    2.471380
2010-12-31    2.337974
2011-12-31    2.083004
2005-01-01    2.000000

However, it seems like it cannot detect that that date is before 2005-12-31 so its just draw a horizontal line from 2005 till 2011. I would really appreciate if you can help me.

Unfortunately, I am not able to upload the graphs, since I am working on a different server and I am not able to save images.

Thank you.

Edition:

Here is the code I used:

plt.figure()
plt.plot(df_month.index,  df_month, 'k')
plt.plot(df_year.index,  df_year, 'g')
plt.show()

Solution

  • If I understood correctly you want a bar plot or steps like plit for you values using dates as x axis.

    DataFrame

    If we set the DataFrameas follows:

    import pandas as pd
    import matplotlib.pyplot as plt
    df = pd.DataFrame([["2005-12-31", 3.347463],["2006-12-31", 3.042220],["2007-12-31", 3.296574],["2008-12-31", 3.082333],["2009-12-31", 2.471380],["2010-12-31", 2.337974],["2011-12-31", 2.083004]])
    df.columns = ["date", "value"]
    df["date"] = pd.to_datetime(df["date"], format="%Y-%m-%d")
    df = df.set_index(["date"])
    

    Your DataFrame would be:

    >>> df
                   value
    date                
    2005-12-31  3.347463
    2006-12-31  3.042220
    2007-12-31  3.296574
    2008-12-31  3.082333
    2009-12-31  2.471380
    2010-12-31  2.337974
    2011-12-31  2.083004
    

    Note that we set the date column as index.

    Using plot.bar

    You can use plot.bar function. If it's not available due to pandas version you can try plot(kind="bar") instead. Below code would plot and show the desired graph:

    df.plot.bar(width=1,fill=False)
    plt.tight_layout()
    plt.show()
    

    And the output graph would result into: Bar Plot

    Note that using width as 1 we get the bars with whole width. width by default is 0.5.

    Using plot with steps-mid as linetyle

    Otherwise you can use plot with steps-mid as linestyle with below code:

    df.plot(ls="steps-mid")
    plt.show()
    

    You get as graph: Plot with steps