2

I have 2 numpy arrays with (time and date), and the third with rain. At the end I would like to plot all the info at a xy-plot with matplotlib! This i what I got so far

import os
import time
from datetime import datetime
import time
import numpy as np
import matplotlib.dates as mdates
import matplotlib.pyplot as plt


date = np.array(["01.06.2015", "01.06.2015", "01.06.2015"], dtype=object)
time = np.array(["12:23:00", "14:54:00", "14:56:00"], dtype=object)
# Rain
rain = np.array([2.544, 1.072, 1.735]

# Calculations to make one array of time and date,
# called timestamp
A = np.vstack((date, time))
A_transp = A.transpose()
A_transp.shape
A_transp.type

So at the end as mentioned I would like to have an (x,y)-Plot, with timestamps(so time and date combined as an array of floating point numbers and the rain on the other axes.

Thank you for your help

Markus

Thank you for your help, but I do not come to a conclusion! Further stepps I did!

# Get a new .out file, to get a time tuple
# see strptime. 
# Finally I would like to make a floating point number out of the 
# timetuple, to plot the hole thing! 
#
mydata = np.savetxt('A_transp.out', A_transp
       ,fmt="%s")
# Dateconv
dateconv = lambda s: datetime.strptime(s, '%d.%m.%Y %H:%M:%S')
# ColNames
col_names = ["Timestamp"]
# DataTypes
dtypes = ["object"]
# Read in the new file
mydata_next = np.genfromtxt('A_transp.out', delimiter=None,
     names=col_names, dtype=dtypes, converters={"Timestamp":dateconv})

So after the np.genfromtxt following error message appears

Traceback (most recent call last):
File "parsivel.py", line 155, in <module>
names=col_names, dtype=dtypes, converters={"Timestamp":dateconv})
File "/home/unix/anaconda2/lib/python2.7/site-
packages/numpy/lib/npyio.py", line 1867, in genfromtxt
output = np.array(data, dtype)
ValueError: Setting void-array with object members using buffer.

What I would try after that would be the following.

#B = mdates.strpdate2num(mydata_next)   # fail
#B = time.mktime(mydata_next)           # fail
#B = plt.dates.date2num(mydata_next)    # fail

And finally I would like to plot the following

# Plot
# Fail
#plt.plot_date(mydata_next, rain)
#plt.show()

But at the moment all the plots fail, because I can not make a time tuple out of A_transp! Maybe also the strptime function is not right here, or there is another way as the detour via np.savetxt and the try of rearanging A_transp?

Markus
  • 189
  • 1
  • 1
  • 10
  • Have you looked at the documentation for plot? http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot – code11 Sep 26 '16 at 13:01
  • Please take your time and read the documentation and eventual tutorials. If and when you encounter a problem you can post it here and we will try to help you. – Pax Vobiscum Sep 26 '16 at 13:04
  • @code11 Thank you for your help, but I do not come to a conclusion! Further stepps I did are in the edited version! Thxs – Markus Sep 26 '16 at 16:08

1 Answers1

0

Starting from your original date and time arrays, you can obtain a date-time string representation in a single array just by adding them:

In[61]: date_time = date + time

In[62]: date_time
Out[62]: array(['01.06.201512:23:00', '01.06.201514:54:00', '01.06.201514:56:00'], dtype=object)

Now you can convert the date-time strings into datetime format. For example:

In[63]: date_time2 = [datetime.strptime(d, '%d.%m.%Y%H:%M:%S') for d in date_time]

In[64]: date_time2
Out[64]: 
[datetime.datetime(2015, 6, 1, 12, 23),
 datetime.datetime(2015, 6, 1, 14, 54),
 datetime.datetime(2015, 6, 1, 14, 56)]

And that's all you need to plot your data with:

plt.plot_date(date_time2, rain)
lodebari
  • 242
  • 6
  • 18