![]() |
dates on graph - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: dates on graph (/thread-18650.html) |
plot list bar graph - mcgrim - May-22-2019 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: 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: I am not sure what else to try.
RE: plot list bar graph - mcgrim - May-23-2019 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 datetime object location - mcgrim - May-25-2019 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 RE: datetime object location - mcgrim - May-25-2019 anyone? Are more info needed? dates on graph - mcgrim - May-25-2019 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() showing dates and times separately in the same x-axis. - mcgrim - May-26-2019 I am trying to obtain a graph like this one (or very similar) http://ctr.maths.lu.se/na/courses/NUMA01/course_media/material/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() RE: showing dates and times separately in the same x-axis. - Yoriz - May-26-2019 Is this the same question as https://python-forum.io/Thread-dates-on-graph ? RE: showing dates and times separately in the same x-axis. - mcgrim - May-26-2019 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. RE: dates on graph - Yoriz - May-26-2019 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. RE: showing dates and times separately in the same x-axis. - Yoriz - May-26-2019 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. |