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

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