May-26-2019, 02:24 PM
I am trying to obtain a graph like this one (or very similar)
http://ctr.maths.lu.se/na/courses/NUMA01.../birds.pdf.
As you can see, the x-axis contains the dates and is separated by the hours.
My graph presents some issues:
first of all, the date format (year-month-day)
will only remain such if I leave the bars width unchanged, but once I try to make them thinner,which is needed to distinguish them,
the format will immediately change to (month-day-hour).
Secondly, I am trying to divide the daylight and night phases (the graph in the link above is separated in
white and light yellow background-columns) but I am not sure how to do that.
If more info are needed, please ask.
http://ctr.maths.lu.se/na/courses/NUMA01.../birds.pdf.
As you can see, the x-axis contains the dates and is separated by the hours.
My graph presents some issues:
first of all, the date format (year-month-day)
will only remain such if I leave the bars width unchanged, but once I try to make them thinner,which is needed to distinguish them,
the format will immediately change to (month-day-hour).
Secondly, I am trying to divide the daylight and night phases (the graph in the link above is separated in
white and light yellow background-columns) but I am not sure how to do that.
If more info are needed, please ask.
from astral import Astral from scipy import * from pylab import* import numpy as np from numpy import array import matplotlib.pyplot as plt import datetime from datetime import timezone from datetime import timedelta import matplotlib.dates as dates import pandas as pd import pytz import sys def last_digits(num, last_digits_count=2): return abs(num) % (10**last_digits_count) orig_date=[] orig_time=[] movements=[] with open('bird_jan25jan16.txt', 'r') as f: for line in f: data = line.split() # Splits on whitespace orig_date.append(data[0][:]) orig_time.append((data[1][:])) movements.append(int(data[2][:])) for i in range(0,len(orig_date)): if ((len(str(movements[i-1])) - len(str(movements[1]))) >=2): if movements[i]==0 or ( (movements[i-1] == movements[i+1] ) and (last_digits(movements[i-1]) == last_digits(movements[i]))): movements[i]=((movements[i-1]+movements[i+1])/2) """ putting date and time together and converting them to datetime objects""" dt_fmt = '%Y-%m-%d %H:%M:%S.%f' timestamps = [] for col_dt in zip(orig_date , orig_time): new_dt_str = ' '.join(col_dt) new_dt = datetime.datetime.strptime(new_dt_str, dt_fmt) timestamps.append(new_dt) def convert_local_timezone(): converted_dates=[] for date in timestamps: local_tz = pytz.timezone('Europe/Berlin') local_time = date.replace(tzinfo=pytz.utc).astimezone(local_tz) converted_dates.append(local_time) return converted_dates CEU_times=convert_local_timezone() def date_split(i): for i in range(len(orig_date)): return CEU_times[i].year , CEU_times[i].month, CEU_times[i].day def time_split(i): for i in range(len(orig_date)): return CEU_times[i].hour , CEU_times[i].minute, CEU_times[i].second def mov_index(i,j): return sum(movements[i:j]) position1 = orig_date.index(("2015-01-25")) position2 = orig_date.index('2015-01-26') position3 = orig_date.index('2015-01-27') print(type(CEU_times[0])) print(CEU_times[position1].date()) # it correctly prints the date y = [mov_index(position1, position1+10), mov_index(position2, position2+10), mov_index(position3, position3+10)] x = [CEU_times[position1] + datetime.timedelta(days=i) for i in range(len(y))] ax = plt.subplot(111) ax.bar(x, y, width=5) ax.xaxis_date() plt.setp(ax.get_xticklabels(), rotation=45, fontsize=10) plt.show()