Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dates on graph
#6
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.

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()
Reply


Messages In This Thread
plot list bar graph - by mcgrim - May-22-2019, 09:16 PM
RE: plot list bar graph - by mcgrim - May-23-2019, 08:53 AM
datetime object location - by mcgrim - May-25-2019, 08:10 PM
RE: dates on graph - by Yoriz - May-27-2019, 06:28 AM
RE: datetime object location - by mcgrim - May-25-2019, 10:31 PM
dates on graph - by mcgrim - May-25-2019, 11:03 PM
RE: dates on graph - by Yoriz - May-26-2019, 03:44 PM
RE: dates on graph - by mcgrim - May-26-2019, 03:57 PM
RE: dates on graph - by Yoriz - May-26-2019, 04:00 PM
RE: dates on graph - by mcgrim - May-26-2019, 04:05 PM
RE: dates on graph - by Yoriz - May-26-2019, 04:13 PM
RE: dates on graph - by mcgrim - May-26-2019, 05:08 PM
RE: dates on graph - by Yoriz - May-26-2019, 06:32 PM
RE: dates on graph - by mcgrim - May-26-2019, 07:53 PM
RE: dates on graph - by Yoriz - May-26-2019, 11:00 PM
showing dates and times separately in the same x-axis. - by mcgrim - May-26-2019, 02:24 PM
RE: datetime object location - by micseydel - May-27-2019, 12:22 AM
RE: plot list bar graph - by nilamo - May-29-2019, 09:26 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020