2

This might be similar to this problem but mine is in the context of plot_date.

UPDATE

log = row[3]

day = log.strftime("%Y%m%d")
month = log.strftime("%m")
month2 = log.strftime("%B")
year = log.strftime("%y")
times = to_seconds(log.strftime("%I:%M"))

log is from the database records in 2016-10-28 20:53:26 format.

time3 =[29100.0, 28860.0, 29280.0, 37980.0, 29100.0, 24780.0, 29700.0, 29160.0, 29460.0, 29100.0, 28980.0, 33780.0, 29400.0, 29100.0, 29400.0, 30360.0, 6180.0, 28860.0, 29400.0, 4440.0, 29160.0, 29160.0]

day3 = ['20161003', '20161004', '20161005', '20161006', '20161007', '20161009', '20161010', '20161011', '20161012', '20161013', '20161014', '20161017', '20161018', '20161019', '20161020', '20161021', '20161023', '20161024', '20161025', '20161026', '20161027', '20161028']

plt.grid(True, which='both')
plt.grid(b=True, which='major', color='k', linestyle='-')
plt.grid(b=True, which='minor', color='k', linestyle='-', linewidth=0.1)
plt.xticks(rotation='vertical',fontsize='small')
plt.minorticks_on()
plt.margins(0.01)

plt.plot_date(x=day3,y=time3,color="red", linestyle='--', markersize=4)
plt.ylabel("Time")
plt.xlabel("Day")

attached is the sample output:

enter image description here

I want to display the y-axis in H:m format.

Community
  • 1
  • 1
user1693411
  • 109
  • 7
  • Possible duplicate of [Python Time Seconds to h:m:s](http://stackoverflow.com/questions/775049/python-time-seconds-to-hms) – Jalo Nov 23 '16 at 09:12
  • Can you elaborate more Jalo. Thank you. – user1693411 Nov 23 '16 at 09:26
  • Alright. But I need more information about your code, as I am not able to reproduce your plot, in order to add the improvements – Jalo Nov 23 '16 at 09:27
  • I used `import matplotlib.pyplot as plt` on the top of it. It's will work. – user1693411 Nov 23 '16 at 09:31
  • The format of your day3 dates is not valid when I run it, so I guess that you have additional instructions – Jalo Nov 23 '16 at 09:57
  • Code works mine, but see update. – user1693411 Nov 23 '16 at 10:10
  • `from matplotlib.dates import DateFormatter from matplotlib.dates import HourLocator ax = plt.subplot() ax.yaxis.set_major_locator(HourLocator()) ax.yaxis.set_major_formatter(DateFormatter('%H:%M')) plt.plot_date(x=adlaw3,y=oras3,color="red", linestyle='--', markersize=4) plt.show() ` this is so far what I've tried still no good. – user1693411 Nov 23 '16 at 10:55

1 Answers1

1

Here is a working solution. Since I don't know how the database relates to the values in days3and time3 I ignored that part. The time3 is given in seconds, however, plot_dates needs datetime-objects, so we need to convert that first. The format of days3 was not useful, so I created respective datetime objects from those values.

In the plotting it is crucial to set the DateFormatter after the call to plot_dates.

import matplotlib.pyplot as plt
import matplotlib.dates
import datetime


time3 =[29100.0, 28860.0, 29280.0, 37980.0, 29100.0, 24780.0, 29700.0, 29160.0, 29460.0, 29100.0, 28980.0, 33780.0, 29400.0, 29100.0, 29400.0, 30360.0, 6180.0, 28860.0, 29400.0, 4440.0, 29160.0, 29160.0]
#time3 is in seconds, but we need it as a datetime object
time3 = [datetime.datetime(1970,1,1) + datetime.timedelta(seconds=time) for time in time3]

# days3 are some (but not all) dates in october, create datetime objects as required
days3 = []
for d in range(3,29):
    if d not in [8,15,16,22]:
        days3.append(datetime.date(2016,10,d))


plt.grid(True, which='both')
plt.grid(b=True, which='major', color='k', linestyle='-')
plt.grid(b=True, which='minor', color='k', linestyle='-', linewidth=0.1)
plt.xticks(rotation='vertical',fontsize='small')
plt.minorticks_on()
plt.margins(0.01)

plt.plot_date(x=days3,y=time3,color="red", linestyle='--', markersize=4)
plt.gca().yaxis.set_major_formatter(matplotlib.dates.DateFormatter('%H:%M')) 
plt.ylabel("Time")
plt.xlabel("Day")
plt.tight_layout()
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712