2
time_arr =[1491343400.0, 1491343830.0, 1491344137.0, 1491344322.0, 1491344751.0, 1491412847.0, 1491413092.0, 1491413277.0, 1491413645.0, 1491413952.0, 1491414198.0, 1491414627.0, 1491418003.0, 1491418924.0, 1491419170.0, 1491419907.0, 1491420091.0, 1491421073.0, 1491422915.0, 1491423591.0, 1491423960.0, 1491424389.0, 1491426048.0, 1491426846.0, 1491427890.0, 1491428566.0, 1491428935.0]

Hi,

I am trying to plot this time array against a volume one of the same length. I would like the time in this format 2015-02-18 02:08:12. My problem:

  1. Converting these seconds to this format :2015-02-18 02:08:12

  2. Using matplotlib to plot TIME and DATES on the x-axis

I got this code from the internet, but it only gives the h, m and s:

def format_seconds_to_hhmmss(seconds):
    hours = seconds // (60*60)
    seconds %= (60*60)
    minutes = seconds // 60
    seconds %= 60
    return "%02i:%02i:%02i" % (hours, minutes, seconds)   

how do I change this to do what I want?

I would like to use a bar graph.

Serenity
  • 35,289
  • 20
  • 120
  • 115
che
  • 73
  • 1
  • 9
  • 3
    Possible duplicate of [Plotting timestamps (hour/minute/seconds) with Matplotlib](http://stackoverflow.com/questions/17709823/plotting-timestamps-hour-minute-seconds-with-matplotlib) – Serenity Apr 20 '17 at 09:34

1 Answers1

3

You have to convert your timestamps to datetime and then use matplotlib to plot it in desired way:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

time_arr =[1491343400.0, 1491343830.0, 1491344137.0, 1491344322.0, 1491344751.0, 1491412847.0, 1491413092.0, 1491413277.0, 1491413645.0, 1491413952.0, 1491414198.0, 1491414627.0, 1491418003.0, 1491418924.0, 1491419170.0, 1491419907.0, 1491420091.0, 1491421073.0, 1491422915.0, 1491423591.0, 1491423960.0, 1491424389.0, 1491426048.0, 1491426846.0, 1491427890.0, 1491428566.0, 1491428935.0]

# convert timestamp to datetime
x = [datetime.datetime.fromtimestamp(val) for val in time_arr]
print(x)
# random y values
y = np.random.rand(len(time_arr))

ax = plt.subplot(111)
ax.bar(x, y, width = 0.01)
# set datetime formatter for x axis
xfmt = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
# set ticks every 30 mins 
ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=30))
# set fontsize of ticks
ax.tick_params(axis='x', which='major', labelsize=5)
# rotate ticks for x axis
plt.xticks(rotation=90)
plt.show()

enter image description here You have a lot of data and ticks are set every 6 hours.

However if print x array you get date and time till seconds:

[datetime.datetime(2017, 4, 5, 8, 3, 20), datetime.datetime(2017, 4, 5, 8, 10, 30), datetime.datetime(2017, 4, 5, 8, 15, 37), datetime.datetime(2017, 4, 5, 8, 18, 42), datetime.datetime(2017, 4, 5, 8, 25, 51), datetime.datetime(2017, 4, 6, 3, 20, 47), datetime.datetime(2017, 4, 6, 3, 24, 52), datetime.datetime(2017, 4, 6, 3, 27, 57), datetime.datetime(2017, 4, 6, 3, 34, 5), datetime.datetime(2017, 4, 6, 3, 39, 12), datetime.datetime(2017, 4, 6, 3, 43, 18), datetime.datetime(2017, 4, 6, 3, 50, 27), datetime.datetime(2017, 4, 6, 4, 46, 43), datetime.datetime(2017, 4, 6, 5, 2, 4), datetime.datetime(2017, 4, 6, 5, 6, 10), datetime.datetime(2017, 4, 6, 5, 18, 27), datetime.datetime(2017, 4, 6, 5, 21, 31), datetime.datetime(2017, 4, 6, 5, 37, 53), datetime.datetime(2017, 4, 6, 6, 8, 35), datetime.datetime(2017, 4, 6, 6, 19, 51), datetime.datetime(2017, 4, 6, 6, 26), datetime.datetime(2017, 4, 6, 6, 33, 9), datetime.datetime(2017, 4, 6, 7, 0, 48), datetime.datetime(2017, 4, 6, 7, 14, 6), datetime.datetime(2017, 4, 6, 7, 31, 30), datetime.datetime(2017, 4, 6, 7, 42, 46), datetime.datetime(2017, 4, 6, 7, 48, 55)]
Serenity
  • 35,289
  • 20
  • 120
  • 115
  • @che Using a bar plot for those unequally spaced data may not make too much sense, but in any case, a smaller bar width is probably desired, like `ax.bar(x, y, ec="k", width=0.005,alpha=0.5)`. – ImportanceOfBeingErnest Apr 20 '17 at 10:28
  • Hi, how can i plot the x-axis values only every half hour? – che Apr 21 '17 at 07:20
  • Use MinuteLocator(interval=30) as above. – Serenity Apr 21 '17 at 07:41
  • thank you, I now have another problem I could use your help on please. – che Apr 21 '17 at 09:34
  • C:\Users\Cheroline\Anaconda3\lib\site-packages\matplotlib\axis.py:1045: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed. warnings.warn("Unable to find pixel distance along axis " – che Apr 21 '17 at 09:34
  • link to the full question: http://stackoverflow.com/questions/43538981/unable-to-find-pixel-distance-along-axis-for-interval-padding-of-ticks-assuming – che Apr 21 '17 at 09:36