I am trying to put in a bar graph some values stored in a file and the times when these values took place.
The bar chart looks like the way I want as long as I have it without dates, but once I introduce plt.xticks(date_list) in the second last line of my code, the graph turns blank.
How do I fix this?
Also, how would be possible to introduce a second xticks to divide this axis into hours as well?
I would like to ask you that in case I am not being clear or more info is needed or whatever issue you may have with my line of questioning, let me please know what in my thread should be fixed.
I included an attached file containing the data I am working with.
I would only need you to refer to the last part of the code dealing with graphing, even though I am posting the whole code here, which is needed in order to run it properly.
The bar chart looks like the way I want as long as I have it without dates, but once I introduce plt.xticks(date_list) in the second last line of my code, the graph turns blank.
How do I fix this?
Also, how would be possible to introduce a second xticks to divide this axis into hours as well?
I would like to ask you that in case I am not being clear or more info is needed or whatever issue you may have with my line of questioning, let me please know what in my thread should be fixed.
I included an attached file containing the data I am working with.
I would only need you to refer to the last part of the code dealing with graphing, even though I am posting the whole code here, which is needed in order to run it properly.
import numpy as np import matplotlib.pyplot as plt import datetime import pytz dt_fmt = '%Y-%m-%d %H:%M:%S.%f' orig_date=[] orig_time=[] movements=[] with open('moredates.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][:])) 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(): """ conversion from strings to datetime objects""" 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() def mov_index(i,j): a=[movements[i] - movements[i-1] for i in range(i,j,30)] # function calculating return a # the values position1 = orig_date.index(('2015-05-12')) position2 = orig_date.index('2015-05-13') position3 = orig_date.index('2015-05-14') start_date = CEU_times[position1].date() y=[mov_index(position1,position3)][0][:] date_list = [start_date + datetime.timedelta(days=x) for x in range(0, 3)] plt.bar(np.arange(len(y[0:-1])),y[0:-1], width=1, color='blue' ) plt.xticks(date_list) # if this line is removed, the graph looks ok plt.show()
Attached Files
moredates.txt (Size: 75.2 KB / Downloads: 7)