Posts: 203
Threads: 41
Joined: Mar 2019
Dear Yoriz,
as I mentioned in the other thread, this thread is only a subset of the one in the other section.
Kindly refer to the one in the general questions since is more complete, and I will detail the case there.
Posts: 2,164
Threads: 35
Joined: Sep 2016
This is why you don't create unnecessary duplicate threads, these threads are going to be merged please don't open a new one.
Posts: 203
Threads: 41
Joined: Mar 2019
Hi Yoriz,
this part of the code seems to be the problem"
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() If I try to change the width of the bar, that seems to change the format of the date I see on the graph.I want to be able to see year-month-date instead of month-date-hour, and I want the x-axis to be separated by hours (for a better Idea if what I mean, kindly refer to the link http://ctr.maths.lu.se/na/courses/NUMA01.../birds.pdf). So far, the only way for me to be able to see the dates in the right format, is to keep the width of the bars to a minimum of 5, which is too much if I want them to be separated and have a better picture. If I lower that 5, the date format in the graph immediately changes to month-day-hour.
Let me please know if additional info are needed.
Posts: 2,164
Threads: 35
Joined: Sep 2016
Ok great, now hard code the missing variables and add the missing required imports so this small snippet of code will run for others to execute it and see the results you are seeing.
Posts: 203
Threads: 41
Joined: Mar 2019
May-26-2019, 05:08 PM
(This post was last modified: May-26-2019, 06:16 PM by Yoriz.)
I am afraid that the whole code (the first one posted above) is needed.
Why can't you refer to/ run that?
If you do so, you will see the results I get.
The file I am using is too large to upload, so down here is a sample.
Let me please know if this is enough.
Again, if you run the first code I posted, it shouldn't be an issue for anyone
to see the results I am seeing.
Output: 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
2015-01-25 15:18:05.527247 70
2015-01-25 15:20:05.431037 70
2015-01-25 15:22:05.829434 70
2015-01-25 15:24:05.534377 70
2015-01-25 15:26:04.839560 70
2015-01-25 15:28:06.531103 70
2015-01-25 15:30:04.756892 70
2015-01-25 15:32:05.739886 70
2015-01-25 15:34:04.749130 70
2015-01-25 15:36:04.777022 70
2015-01-25 15:38:05.768101 74
2015-01-25 15:40:05.969609 74
2015-01-25 15:42:05.575432 74
2015-01-25 15:44:06.074641 74
2015-01-25 15:46:05.412341 74
2015-01-25 15:48:05.350484 74
2015-01-25 15:50:06.534232 74
2015-01-25 15:52:05.386500 74
2015-01-25 15:54:06.184306 74
2015-01-25 15:56:05.496602 75
2015-01-25 15:58:05.397928 75
2015-01-25 16:00:05.897354 75
2015-01-25 16:02:05.500175 75
2015-01-25 16:04:04.806863 75
2015-01-25 16:06:05.008453 75
2015-01-25 16:08:05.208770 75
2015-01-25 16:10:05.708145 75
2015-01-25 16:12:05.609773 75
2015-01-25 16:14:04.775696 75
2015-01-25 16:16:05.416838 75
2015-01-25 16:18:05.916246 75
2015-01-25 16:20:06.090367 75
2015-01-25 16:22:06.616131 75
2015-01-25 16:24:04.728852 75
2015-01-25 16:26:04.929489 75
here is the code that I am now running, and I am getting the outcome described before.
rom 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 mov_index(i,j):
return sum(movements[i:j])
#x = pd.Timestamp('2015-01-25')
#res = next((i for i, j in enumerate(CEU_times[0:5]) if j == x),10)
#print(res)
#print(CEU_times[0].year , CEU_times[0].month , CEU_times[0].day)
position1 = orig_date.index(("2015-01-25"))
position2 = orig_date.index('2015-01-26')
position3 = orig_date.index('2015-01-27')
from matplotlib.dates import date2num
print(type(CEU_times[0]))
print(CEU_times[position1].date()) # it correctly prints the date
city_name = 'Berlin'
a = Astral()
a.solar_depression = 'civil'
city = a[city_name]
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))]
sun1 = (city.sun(date=(CEU_times[position1]), local=True ))
ax = plt.subplot(111)
ax.bar(x, y, width=5) # by lowering the width the dates will have the format (month, day, hour)
ax.xaxis_date()
print('Dawn: %s' % str(sun1['dawn']))
print('Sunset: %s' % str(sun1['sunset']))
plt.setp(ax.get_xticklabels(), rotation=45, fontsize=10)
plt.show()
doesn't anyone know the answer or more info are needed?
Posts: 2,164
Threads: 35
Joined: Sep 2016
May-26-2019, 06:32 PM
(This post was last modified: May-26-2019, 06:36 PM by Yoriz.)
I cant run the code because i don't have astral.
I want to be able to help with the least effort on my part possible.
I would rather not create a text file and install modules just to make code run that's unrelated to the problem.
If you are happy with the code up to the point of it being plotted its not part of the problem.
just before x and y are plotted they have been assigned values from all the previous code,
print out x and y and hard code a sample of it that will show the problem into the following code.
import matplotlib.pyplot as plt
y = [] # contents of the list at this point
x = [] # contents of the list at this point
ax = plt.subplot(111)
ax.bar(
x, y, width=5
) # by lowering the width the dates will have the format (month, day, hour)
ax.xaxis_date()
plt.setp(ax.get_xticklabels(), rotation=45, fontsize=10)
plt.show() now all i have to do to see the same result as you is copy and paste these few lines, i only need to have matplotlib installed, i don't have to mess around with files unnecessarily.
Be aware that even after this, if i can run the code, it doesn't mean i will necessarily have a solution for you, but the easier you make it for everyone to try and help you the more likely you will get responses to the thread.
Posts: 203
Threads: 41
Joined: Mar 2019
May-26-2019, 07:53 PM
(This post was last modified: May-26-2019, 08:02 PM by mcgrim.)
Dear Yoriz,
thanks for your attempt.
A few things I would like to specify:
1- you are right when you say that I am happy with the code up to when I plot it.
2- you don't need astral in order to help me solve this issue, as I didn't include the sunrise time
in my graph yet.
3-I would like to enhance that the main issue that I have with the plot right now, is being able to slim the width of the bars without losing the date format on the graph, meanwhile, showing all the hours marked with small vertical lines. The link I included before will show you the graph that I am trying to obtain.
Let me please know if there is anything else that I should provide.
for the values of y, I am currently trying these three:
700 760 980
and for x, these 3 dates:
2015-01-25, 2015-01-26, 2015-01-27
Posts: 2,164
Threads: 35
Joined: Sep 2016
May-26-2019, 11:00 PM
(This post was last modified: May-26-2019, 11:01 PM by Yoriz.)
You've got to continue playing with this to try and get what you want
I used these references
https://matplotlib.org/gallery/text_labe...ns-date-py
https://matplotlib.org/gallery/ticks_and..._demo.html
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.ticker import AutoMinorLocator
import datetime
years_fmt = mdates.DateFormatter('%Y-%m')
hour = mdates.HourLocator()
hours_fmt = mdates.DateFormatter('%H')
dates = ["2015-01-25", "2015-01-26", "2015-01-27"]
y = [700, 760, 980] # contents of the list at this point
x = [
datetime.datetime.strptime(date, '%Y-%m-%d').date() for date in dates
] # contents of the list at this point
ax = plt.subplot(111)
ax.bar(
x, y, width=5
) # by lowering the width the dates will have the format (month, day, hour)
ax.xaxis_date()
# format the ticks
ax.xaxis.set_major_formatter(years_fmt)
ax.xaxis.set_minor_locator(hour)
ax.xaxis.set_minor_formatter(hours_fmt)
ax.xaxis.set_minor_locator(AutoMinorLocator(8))
plt.setp(ax.get_xticklabels(), rotation=45, fontsize=10)
plt.show()
Posts: 2,343
Threads: 62
Joined: Sep 2016
Can you simplify your question to 5-10 lines of code, no imports, and your file hard-coded into the script? I tend to skip questions that are too much code, and in your cases it's definitely more imports than are needed to reproduce the issue.
Posts: 2,164
Threads: 35
Joined: Sep 2016
May-27-2019, 06:28 AM
(This post was last modified: May-27-2019, 06:34 AM by Yoriz.)
Until mcgrim can ask simplified questions that don't rely on all this code that's irrelevant to the question, i'm merging any threads into this one.
Note: micseydel's post previous to this, is in reply to the the posts by mcgrim that after merging are the first two posts of this thread.
|