I'm trying to plot on a bar graph a year long set of values with Python/Pandas. The resulting graph is quite cluttered and my attempts to set xtick labels are not working.
My original code:
import datetime as dt
import random
import pandas as pd
#generate year-long list of dates
i = 0
list_of_dates = []
last_date = dt.date(2017, 1, 1)
while i < 365:
list_of_dates.append(last_date)
last_date += dt.timedelta(days=1)
#print(last_date)
i = i + 1
#generate random values for example
daily_rainfall = []
j = 0
while j < 365:
daily_rainfall.append(random.randint(0,3))
j = j + 1
#put lists in DF
rainfall_dataframe = pd.DataFrame(list(zip(list_of_dates, daily_rainfall)),columns=["Date","Precipitation"])
rainfall_dataframe = rainfall_dataframe.groupby(["Date"]).sum()
rainfall_dataframe.plot(kind="bar", figsize=(13,7))
returns this:
Unusable, obviously. So I wanted it to only label x-ticks on a monthly basis. I tried creating a list of datetime date objects that was only the first of every month but when I try to pass this to df.plot() it returns nothing.
xlablist = []
xlablist.append(dt.date(2017, 1, 1))
xlablist.append(dt.date(2017, 6, 1))
xlablist.append(dt.date(2018, 1, 1))
rainfall_dataframe.plot(kind="bar", figsize=(13,7), xticks=xlablist)
returns:
Please help!