2

How to set the xaxis in time interval 15minutes as:

'08:00', '08:15', '08:30', '08:45', '09:00', '09:15', '09:30', '09:45', '10:00','10:15', '10:30', '10:45','11:00'

So each one is a label in xaxis.

data is as follows:

data = [[0,1],[08:18,09:16],[10:18,10:52]]

x1 = data[:][1]

x2 = data[:][2]

y = data[:][0]

I want to use plot function to plot with data as above:

plt.plot(data[:][1], y,
         data[:][2], y)

After plotting, there should have two lines on the map. And the labels are in time interval of 15min.

1
  • 1
    What exactly is your data. Because data = [[0,1],[08:18,09:16],[10:18,10:52]] is not valid Python. Dec 18, 2013 at 12:10

1 Answer 1

1
  1. The given data is not valid in Python. It should be like [["0","1"],["08:18","09:16"]].

  2. Write a function to translate the time intervals into real number. (For understandablity to you, the code is not that pythonic.)

    def time_to_num(time_str):
        time_str = time_str.split(':')
            #check if time_str is valid here, then:
        num = 0
        num = int(time_str[0])
        if len(time_str) == 2:
            num += int(time_str[1])/60.0
        return num
    
  3. Make it like x1 = [time_to_num(item[1]) for item in data]

  4. Set the X axis lable as time interval with something like:

    #fig = matplotlib.plot...
    fig.set_xticks([0, 0.25, 0.5, 0.75, 1, 1.25])
    fig.set_xticklabels(['0:00', '0:15', '0:30', '0:45', '1:00', '1:15'])
    
3
  • You could also use datetime to do do the parsing for you. You can then directly plot the datetime objects with maplotlib. setxticklabels should only be used as a last resort, it de-couples your labels from your data.
    – tacaswell
    Dec 18, 2013 at 20:06
  • @tcaswell Yup, it's a kinda dirty hack :-)
    – Timothy
    Dec 18, 2013 at 20:10
  • It works.:) Though setxticklabels here is a hack. I would like to use it.Thanks,Skyler and tcaswell.
    – David Alex
    Dec 19, 2013 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.