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

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

.txt   moredates.txt (Size: 75.2 KB / Downloads: 7)
Reply


Messages In This Thread
xticks - by mcgrim - Jun-06-2019, 09:00 PM
RE: xticks - by mcgrim - Jun-07-2019, 11:32 AM
RE: xticks - by heiner55 - Jun-07-2019, 01:36 PM
RE: xticks - by michalmonday - Jun-07-2019, 01:40 PM
RE: xticks - by Yoriz - Jun-07-2019, 01:56 PM
RE: xticks - by mcgrim - Jun-09-2019, 09:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tell exact difference between xlim() and xticks() function ift38375 3 5,248 Jul-12-2019, 12:04 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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