May-25-2019, 11:03 PM
I am working on the same code and trying to get a bar graph listing the dates, moreover, I am trying to divide the x axis where the dates should show, into hours, and the bars are supposed to represent the movements.
I am not sure why, the graph I obtain shows only the month,day,hour instead of showing year, month, day.
I am also not sure how to divide the x-axis into hours.
Here is my code.
I hope to get some help.
I am not sure why, the graph I obtain shows only the month,day,hour instead of showing year, month, day.
I am also not sure how to divide the x-axis into hours.
Here is my code.
I hope to get some help.
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' #your_dt = datetime.datetime.strptime(d + ' ' +t, dt_fmt) 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 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(CEU_times[position1].date()) # it correctly prints the date x = [CEU_times[position1].date(), # the graph shows month, day and hour CEU_times[position2].date(), # but no year, I would like to see the CEU_times[position3].date()] # date format (year, month, day) y = [mov_index(position1,position1+20),mov_index(position2,position2+20),mov_index(position3,position3+20)] ax = plt.subplot(111) ax.bar(x, y, width=.2) ax.xaxis_date() plt.setp(ax.get_xticklabels(), rotation=45, fontsize=10) plt.show()