0

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:

img1

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:

img2

Please help!

Krbin
  • 33
  • 2
  • 5
  • Does this answer your question? [Pandas bar plot changes date format](https://stackoverflow.com/questions/30133280/pandas-bar-plot-changes-date-format) – baccandr Aug 26 '20 at 08:39

2 Answers2

1

One of possible solutions, using MonthLocator to specify where to put x labels and DateFormatter to specify the format of labels:

# Imports
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

# Create source data
np.random.seed(0)
dates = pd.date_range(start='2017-01-01', end='2017-12-31')
rainfall = np.random.randint(0, 20, dates.size)

# Drawing
fig, ax = plt.subplots(figsize=(10, 4))
plt.xlabel('Month')
plt.ylabel('mm')
plt.title('Rainfall 2017')
ax.xaxis.set_major_locator(mdates.MonthLocator())
fmt = mdates.DateFormatter('%b %Y')
ax.xaxis.set_major_formatter(fmt)
ax.bar(dates, rainfall)
plt.setp(ax.get_xticklabels(), rotation=30);

For the above source data I got the following picture:

enter image description here

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
0

You could use Date locators.

import datetime as dt
import random
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates


#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))

plt.gca().xaxis.set_major_locator(mdates.MonthLocator())

plt.xticks(rotation=45)

plt.show()

enter image description here

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • I tried running your code exactly in a fresh Jupyter Notebook and the xticks didn't show up. Is there a problem with my install of matplotlib or something? – Krbin Aug 26 '20 at 03:56
  • I don't use Jupyter Notebook. Does it work on your Python IDLE? – Ynjxsjmh Aug 26 '20 at 04:04
  • @Krbin Does this official demo: [Date tick labels](https://matplotlib.org/gallery/text_labels_and_annotations/date.html?highlight=monthlocator) work in your Jupyter Notebook? – Ynjxsjmh Aug 26 '20 at 04:08