Created
April 15, 2021 18:01
-
-
Save pas-calc/aa5e2637690854d8c9c19661f36e8704 to your computer and use it in GitHub Desktop.
Plot dates in histogram from clipboard by month (Python2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import numpy as np | |
# ~from io import StringIO | |
import matplotlib.pyplot as plt | |
import pyperclip | |
from datetime import datetime | |
import matplotlib.dates as mdates | |
from matplotlib.dates import date2num | |
from dateutil.relativedelta import relativedelta | |
def getdate(s): | |
return s.strip().split(' ')[0].strip() | |
def datestr2dateobj(datestr): | |
try: | |
return datetime.strptime(datestr, '%d.%m.%Y') # '%d.%m.%Y %H:%M:%S' | |
except ValueError: | |
return None | |
def getmonthdelta(end_date,start_date): | |
return (end_date.year - start_date.year) * 12 + (end_date.month - start_date.month) | |
cliptext = pyperclip.paste() | |
# ~cliptext = "5.1.2019\n10.1.2019\n11.01.2019\n3.2.2019\n3.3.2019\n4.3.2019\n13.3.2019" | |
data = cliptext.split("\n") | |
# ~data = np.genfromtxt(StringIO(cliptext), delimiter="\t", dtype=None, encoding=None) | |
# ~print data.shape | |
# ~data = data[:,0] # reduce to one column if there are multiple | |
dates = [datestr2dateobj(getdate(date)) for date in data] | |
dates = [x for x in dates if x] | |
print "data: len =", len(dates) | |
if (not dates): | |
print "no data, exiting" | |
sys.exit() | |
date_min = min(dates) | |
date_max = max(dates) | |
print "min = ",date_min | |
print "max = ",date_max | |
md = getmonthdelta(date_max,date_min) | |
print "month between: ",md | |
# define first bin to plot | |
nullmonth = date_min.replace(day=1) | |
# get a list of all months to bin in histogram | |
monthlist = [] | |
onemonth = relativedelta(months=1) | |
for i in range(md+1): | |
monthlist.append(nullmonth + i*onemonth) # alternative: add one month until the end date is passed | |
monthlist_plot = date2num(monthlist) | |
print "month list: len=", len(monthlist),', min= ', min(monthlist),', max= ', max(monthlist) | |
# ~print monthlist_plot | |
# ~print "dates= ",dates | |
mpl_data = date2num(dates) | |
# ~# PLOT | |
fig, ax = plt.subplots() | |
ax.hist(mpl_data, bins = monthlist_plot) | |
plt.title("histogram") | |
# format the ticks | |
years = mdates.YearLocator() # every year | |
months = mdates.MonthLocator() # every month | |
years_fmt = mdates.DateFormatter('%Y') | |
month_fmt = mdates.DateFormatter('%m') | |
ax.xaxis.set_major_locator(years) | |
ax.xaxis.set_major_formatter(years_fmt) | |
ax.xaxis.set_minor_locator(months) | |
ax.xaxis.set_minor_formatter(month_fmt) | |
# ~ax.xaxis.set_tick_params(rotation=45) | |
ax.tick_params(axis='x', rotation=90) | |
fig.autofmt_xdate(bottom=0.2, rotation=30, ha='right') | |
plt.show() | |
# ~plt.hist(mpl_data, bins = monthlist_plot, ec="k") | |
# ~plt.gcf().autofmt_xdate() | |
# ~plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment