Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dates on graph
#1
I am trying to plot a bar graph with the 'movements' represented.
Here is my code
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

file=open('bird_jan25jan16.txt','r')

#Turning datas in file into lists
orig_date=[]
orig_time=[]
movements=[]

for i in file:
    tempsplit=i.split(' ')
    orig_date.append(tempsplit[0])
    orig_time.append(tempsplit[1])
    movements.append((tempsplit[5]))


#d = [datetime.strptime(date, "%Y-%m-%d").date() for date in orig_date] #string to datetime conversion
#t=[datetime.strptime(time, "%H:%M:%S.%f").time() for time in orig_time] #string to datetime conversion
#
    
""" putting date and time together"""

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)
    
#print(timestamps[1].tzinfo)

converted_dates=[]
def convert_local_timezone(converted_dates):

    for date in timestamps:
        local_tz = pytz.timezone('Europe/Copenhagen')
        local_time = date.replace(tzinfo=pytz.utc).astimezone(local_tz)
        converted_dates.append(local_time)
        return converted_dates

CEU_times=convert_local_timezone(timestamps)

print(CEU_times[100:1200])
print(movements[100:1200])
height = [movements[100:1200]]

objects = ('{}'.format(CEU_times[100:1200]))
y_pos = np.arange(len(height))
performance = [movements[100:1200]]

plt.barh(y_pos, performance, align='center', alpha=0.5)
plt.yticks(y_pos, objects)
plt.xlabel('dates')
plt.title('birds')

plt.show()
but I get the following error:

Error:
runfile('C:/Users/Desktop/python ode/birds.py', wdir='C:/Users//Desktop/python ode') Traceback (most recent call last): File "<ipython-input-37-014ce688e577>", line 1, in <module> runfile('C:/Users/Desktop/python ode/birds.py', wdir='C:/Users/Desktop/python ode') File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/Desktop/python ode/birds.py", line 71, in <module> plt.barh(y_pos, performance, align='center', alpha=0.5) File "C:\Users\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2795, in barh ret = ax.barh(*args, **kwargs) File "C:\Users\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 2487, in barh bottom=y, **kwargs) File "C:\Users\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 1867, in inner return func(ax, *args, **kwargs) File "C:\Users\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 2283, in bar label='_nolegend_', File "C:\Users\Anaconda3\lib\site-packages\matplotlib\patches.py", line 686, in __init__ Patch.__init__(self, **kwargs) File "C:\Users\Anaconda3\lib\site-packages\matplotlib\patches.py", line 95, in __init__ self.set_linewidth(linewidth) File "C:\Users\Anaconda3\lib\site-packages\matplotlib\patches.py", line 362, in set_linewidth self._linewidth = float(w) TypeError: only size-1 arrays can be converted to Python scalars
I thought that the problem was 'movements.append((tempsplit[5]))'
that's why I added : "movements.append(int(tempsplit[5])))

but if I do so, I get another error:

Error:
runfile('C:/Users/Desktop/python ode/birds.py', wdir='C:/Users/Desktop/python ode') Traceback (most recent call last): File "<ipython-input-38-014ce688e577>", line 1, in <module> runfile('C:/Users/Desktop/python ode/birds.py', wdir='C:/Users/Desktop/python ode') File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/Desktop/python ode/birds.py", line 32, in <module> movements.append(int(tempsplit[5])) ValueError: invalid literal for int() with base 10: '\n'
I am not sure what else to try.
Reply
#2
here is a subset of the data I'm working with.

2015-01-25 14:05:41.274647 70
2015-01-25 14:08:05.036915 70
2015-01-25 14:10:05.536604 70
2015-01-25 14:12:05.142511 70
2015-01-25 14:14:05.045367 70
2015-01-25 14:16:05.005320 70
2015-01-25 14:18:06.639303 70
2015-01-25 14:20:04.753845 70
2015-01-25 14:22:05.549372 70
2015-01-25 14:24:04.558621 70
2015-01-25 14:26:04.477710 70
2015-01-25 14:28:05.258913 70
2015-01-25 14:30:05.293665 70
2015-01-25 14:32:05.660442 70
2015-01-25 14:34:05.566237 70
2015-01-25 14:36:04.613010 70
2015-01-25 14:38:05.071092 70
2015-01-25 14:40:05.570470 70
2015-01-25 14:42:05.177669 70
2015-01-25 14:44:05.378265 70
2015-01-25 14:46:05.401841 70
2015-01-25 14:48:06.385270 70
2015-01-25 14:50:06.885364 70
2015-01-25 14:52:05.595971 70
2015-01-25 14:54:04.674197 70
2015-01-25 14:56:05.066591 70
2015-01-25 14:58:05.324721 70
2015-01-25 15:00:05.506297 70
2015-01-25 15:02:05.706856 70
2015-01-25 15:04:04.715008 70
2015-01-25 15:06:08.496104 70
2015-01-25 15:08:04.818931 70
2015-01-25 15:10:05.617209 70
2015-01-25 15:12:05.072410 70
2015-01-25 15:14:06.616272 70
2015-01-25 15:16:05.027884 70
Reply
#3
After the conversion to datetime object is made, I am trying to see how to locate them, in other words, I am trying to obtain the index (I already know how to locate strings).

This is the whole code:

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)
    
#    for j in range(0,len(orig_date)):
#        if abs(movements[i-1]-movements[i])>8
            
#print(movements[2],orig_date[0],orig_time[0])


#for j in range(0,len(orig_date)):
#    if ((int(orig_time[j][3:5])+int(orig_time[j-1][3:5])) > 8):
#        max(int(orig_time[j][3:5]),int(orig_time[j][3:5])) =abs( 8 - min(int(orig_time[j][3:5]),int(orig_time[j][3:5])))
        

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


x = pd.Timestamp('2015-01-25') # for this date, the outcome should be 0, since this is the first value
res = next((i for i, j in enumerate(CEU_times) if j == x), None )
print(res)  
The last three lines of the code are the ones supposed to return the location of the datetime object.
It always returns 'None' and I am understanding that it is because of what the second last line says after the comma.
How do I change the code in order to get the location?

here is a sample of the file I am working with:

2015-01-25 14:05:41.274647 70
2015-01-25 14:08:05.036915 70
2015-01-25 14:10:05.536604 70
2015-01-25 14:12:05.142511 70
2015-01-25 14:14:05.045367 70
2015-01-25 14:16:05.005320 70
2015-01-25 14:18:06.639303 70
2015-01-25 14:20:04.753845 70
2015-01-25 14:22:05.549372 70
2015-01-25 14:24:04.558621 70
2015-01-25 14:26:04.477710 70
2015-01-25 14:28:05.258913 70
2015-01-25 14:30:05.293665 70
2015-01-25 14:32:05.660442 70
2015-01-25 14:34:05.566237 70
2015-01-25 14:36:04.613010 70
2015-01-25 14:38:05.071092 70
2015-01-25 14:40:05.570470 70
2015-01-25 14:42:05.177669 70
2015-01-25 14:44:05.378265 70
2015-01-25 14:46:05.401841 70
2015-01-25 14:48:06.385270 70
2015-01-25 14:50:06.885364 70
2015-01-25 14:52:05.595971 70
2015-01-25 14:54:04.674197 70
2015-01-25 14:56:05.066591 70
2015-01-25 14:58:05.324721 70
2015-01-25 15:00:05.506297 70
2015-01-25 15:02:05.706856 70
2015-01-25 15:04:04.715008 70
2015-01-25 15:06:08.496104 70
2015-01-25 15:08:04.818931 70
Reply
#4
anyone?
Are more info needed?
Reply
#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
#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
#7
Is this the same question as https://python-forum.io/Thread-dates-on-graph ?
Reply
#8
the question https://python-forum.io/Thread-dates-on-graph is a subset of this new thread.
I kept working on it and I found more aspects, but still not able to fix it, that's why I posted a brand new thread.
I already mentioned to an administrator my willingness to put these kind of threads into this section (with possibly more visitors), as in the homework section, I haven't got a single answer.
If you (or anyone else) can help me with this, I would truly appreciate it, as I have been spending hours in trying to figure out where the problem is.
Reply
#9
Can the code be reduced just to show minimal code that shows the problem.
Can you remove the need to import a file, just hard code a small sample of data used in the plot.
Is the problem with the data that you send to the plot or with how the plot displays your data.
Reply
#10
Posting new threads for the same question just dilutes responses and makes it confusing.
If no one knows how to answer a thread in homework, recreating it in general won't make any difference, just give admins/moderators more work moving things around.
Reply


Forum Jump:

User Panel Messages

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