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


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