Python / Matplotlib — Histogram of Dates by Day of Year

 ̄綄美尐妖づ 提交于 2020-07-09 03:57:10

问题


I have a list of dates that span several (hundred) years. I'd like to make a histogram that has 366 buckets, one for each day of the year, with the x-axis labelled in a legible way that allows me to see which date is which (I'm expecting a dip for February 29, for example).

I've made the following histogram, but easy-to-read X-axis date labels would be awesome. The following code seems cumbersome but gets me what I want (without the X-axis labels):

from datetime import date, datetime, timedelta
from collections import Counter
import pylab


def plot_data(data):
    """data is a list of dicts that contain a field "date" with a datetime."""

    def get_day(d):
        return d.strftime("%B %d")  # e.g. January 01

    days = []
    n = 366
    start_date = date(2020, 1, 1)  # pick a leap year
    for i in range(n):
        d = start_date + timedelta(days=i)
        days.append(get_day(d))

    counts = Counter(get_day(d['date']) for d in data)

    Y = [counts.get(d) for d in days]
    X = list(range(len(days)))

    pylab.bar(X, Y)
    pylab.xlim([0, n])

    pylab.title("Dates day of year")
    pylab.xlabel("Day of Year (0-366)")
    pylab.ylabel("Count")
    pylab.savefig("Figure 1.png")

Any help to shorten this up and make for more flexible and legible x-axis dates would be much appreciated!


回答1:


Try to check this code:

# import section
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as md
import numpy as np
from datetime import date
from itertools import product

# generate a dataframe like yours
date = [date(2020, m, d).strftime("%B %d") for m, d in product(range(1, 13, 1), range(1, 29, 1))]
value = np.abs(np.random.randn(len(date)))
data = pd.DataFrame({'date': date,
                     'value': value})
data.set_index('date', inplace = True)

# convert index from str to date
data.index = pd.to_datetime(data.index, format = '%B %d')

# plot
fig, ax = plt.subplots(1, 1, figsize = (16, 8))
ax.bar(data.index,
       data['value'])

# formatting xaxis
ax.xaxis.set_major_locator(md.DayLocator(interval = 5))
ax.xaxis.set_major_formatter(md.DateFormatter('%B %d'))
plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90 )
ax.set_xlim([data.index[0], data.index[-1]])

plt.show()

that gives me this plot:

I converted the index of the dataframe from string to date, then I applied the xaxis format that I want through ax.xaxis.set_major_locator and ax.xaxis.set_major_formatter methods.
In order to plot that I used matplotlib, but it should not be difficult to translate this approach to pylab.


EDIT

If you want days and months of separate ticks, you can add a secondary axis (check this example) as in this code:

# import section
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as md
import numpy as np
from datetime import date
from itertools import product
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA

# generate a dataframe like yours
date = [date(2020, m, d).strftime("%B %d") for m, d in product(range(1, 13, 1), range(1, 29, 1))]
value = np.abs(np.random.randn(len(date)))
data = pd.DataFrame({'date': date,
                     'value': value})
data.set_index('date', inplace = True)

# convert index from str to date
data.index = pd.to_datetime(data.index, format = '%B %d')

# prepare days and months axes
fig = plt.figure(figsize = (16, 8))
days = host_subplot(111, axes_class = AA.Axes, figure = fig)
plt.subplots_adjust(bottom = 0.1)
months = days.twiny()

# position months axis
offset = -20
new_fixed_axis = months.get_grid_helper().new_fixed_axis
months.axis['bottom'] = new_fixed_axis(loc = 'bottom',
                                       axes = months,
                                       offset = (0, offset))
months.axis['bottom'].toggle(all = True)

#plot
days.bar(data.index, data['value'])

# formatting days axis
days.xaxis.set_major_locator(md.DayLocator(interval = 10))
days.xaxis.set_major_formatter(md.DateFormatter('%d'))
plt.setp(days.xaxis.get_majorticklabels(), rotation = 0)
days.set_xlim([data.index[0], data.index[-1]])

# formatting months axis
months.xaxis.set_major_locator(md.MonthLocator())
months.xaxis.set_major_formatter(md.DateFormatter('%b'))
months.set_xlim([data.index[0], data.index[-1]])

plt.show()

which produces this plot:




回答2:


Modifying the accepted answer just a bit gives:

locator = md.MonthLocator(bymonthday=(1, 15))
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(md.ConciseDateFormatter(locator))
#plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90 )
ax.set_xlim([data.index[0], data.index[-1]])

plt.show()



来源:https://stackoverflow.com/questions/62391648/python-matplotlib-histogram-of-dates-by-day-of-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!