0

I have formatted data such as this example:

datalist =  [[(u'2017-06-28', 12), (u'2017-06-29', 0), (u'2017-06-30', 17), (u'2017-06-26', 17), (u'2017-06-27', 8), (u'2017-07-02', 0), (u'2017-07-01', 2)], [(u'2017-06-28', 6), (u'2017-06-29', 3), (u'2017-06-30', 13), (u'2017-06-26', 12), (u'2017-06-27', 9), (u'2017-07-02', 1), (u'2017-07-01', 2)]]

which is basically a list of lists of tuples - ([[(date, value), …], [(date, value), …]]. I would like to plot this data such that dates will correspond x-axis labels and values will be heights of my bars for my bar chart. What is the best -or less time consuming way to achieve this? Any help is appreciated. Thanks in advance.

1
  • You should at least make an attempt first. A 3-liner approach would separate your tuples out into two lists representing x and y via 2 list comprehensions and then plot as it's shown in the tutorial. Commented Aug 1, 2017 at 20:31

1 Answer 1

2

If you want a bar plot of the first element of your outermost list, you should get your values into lists using a simple list comprehension, but to convert the date values directly into datetime you can do something like the shown below. If you need the values for the second element, just change datalist[0] to datalist[1].

import datetime
x = [datetime.datetime.strptime(tup[0],"%Y-%m-%d") for tup in datalist[0]]
y = [tup[1] for tup in datalist[0]] 

And then just call plt.bar():

plt.bar(x,y)
plt.show()

To plot multiple bar charts with datetime one can do the following:

x1 = [datetime.datetime.strptime(tup[0],"%Y-%m-%d") for tup in datalist[0]]
x2 = [datetime.datetime.strptime(tup[0],"%Y-%m-%d") for tup in datalist[1]]
y1 = [tup[1] for tup in datalist[0]]
y2 = [tup[1] for tup in datalist[1]]

plt.bar(x1,y1,.1,color='blue')
plt.bar([i+datetime.timedelta(.1) for i in x2],y2,.1,color='red')
plt.show()

If you don't know how many bar you'll need you just need to make the last shown code in a for loop, storing the data in a list of lists(so that, for example, x4 is xarr3), something like this:

xarr = [x1,x2]
yarr = [y1,y2]

for i in range(len(datalist)):
    plt.bar([j+datetime.timedelta((i-1)*.1) for j in xarr[i]],yarr[i], width = .1)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Actually, I might have not stated clearly in the post but what I am trying to do is plotting all the list of tuples in my list -showing all the pairs, for each list in the same plot. To be clearer, my 'datalist' has not a fixed length, and I should plot each list it has in the same graph as I said. Thank you very much for your quick answer though.
You would like something like this then? matplotlib.org/examples/pylab_examples/barchart_demo.html
Yes, only difference is amount of lists I need to process is not fixed instead of being two
@OzanDogrultan Oh, I see, I'll update my answer accordingly.
As you said, there is a tricky part of the problem but your answer is pretty much what I asked. Thank you!
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.