0

I've two lists:

time_str = ['06:03' '06:03' '06:04' ..., '19:58' '19:59' '19:59']
value = ['3.25' '3.09' '2.63' ..., '2.47' '2.57' '2.40']

I tried below code but got error:

    plt.plot(time_str,value)
    plt.xlabel('Time')
    plt.show()

ValueError: invalid literal for float(): 06:00

How can I plot time_str on x_axis and value on y axis. time_str has values for every minute and maybe we can show for every 15 minutes ticks on x axis.I tried in several ways but I couldn't get the line plot properly. Can anyone suggest

Edit: After some trials, I have this code yet I don't have appropriate labels on the axis (It appears as though python just tried to scratch something):

   fig = plt.figure()
    ax = fig.add_subplot(1,1,1)  
    ax.xaxis.set_major_locator(md.MinuteLocator(interval=15))
    ax.xaxis.set_major_formatter(md.DateFormatter('%H:%M')) 
    plt.plot(y)
    plt.xticks(range(len(x)), x)
    plt.show()

enter image description here

Master_Roshy
  • 1,245
  • 3
  • 12
  • 19
  • plt.plot(list(range(len(time_str))), value) plt.xticks(range(len(time_str)), time_str, rotation=90) plt.xticks(range(1680),time_str, 15.0) #ax.set_xticklabels(time_str) – Master_Roshy May 05 '16 at 17:47

1 Answers1

1

You can plot every ith value using numpy's array slicing and indexing (http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#basic-slicing-and-indexing)

#only plot every 15th value
plt.plot(time_str[::15], value[::15]) 

In response to question update
The value error is not associated with the size of the list. You need to cast your string as a datetime object. Something like:

from datetime import datetime
times = [datetime.strptime(time, '%I:%M') for time in  time_str]

In response to comment
You will have to adjust this to your needs, but using masks would be the simpliest way to get every 15 mins

import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np

time_str = ['06:03', '06:18', '06:28', '19:33', '19:03', '19:59']
value = np.array(['3.25', '3.09', '2.63', '2.47', '2.57', '2.40'])

times = np.array([datetime.strptime(time, '%H:%M') for time in  time_str])
time_deltas = np.array([(time - times[0]).total_seconds()/60. for time in times])

plt_times = times[time_deltas%15==0]
plt_values = value[time_deltas%15==0]

plt.plot_date(plt_times, plt_values)
plt.show()
Ben
  • 6,986
  • 6
  • 44
  • 71
  • I think plot need float values for x axis. I have tried converting to datetime and used plot_date. Just to correct my question instead of every 15th value how can I take every 15 min's – Master_Roshy May 05 '16 at 18:05
  • Also I got this error when I tried to convert to datetime: TypeError: must be string, not module – Master_Roshy May 05 '16 at 18:10
  • Thanks for the help. Just a correction, I want to plot all the values but the ticks on the x axis I want to display only every 15 minutes. I guess your code keeps only data which is every 15 minutes – Master_Roshy May 06 '16 at 11:45
  • Got it. I used code from here: http://stackoverflow.com/questions/25538520/change-tick-frequency-on-x-time-not-number-frequency-in-matplotlib – Master_Roshy May 06 '16 at 11:59