0

code for ploting bar chart:

import pylab as pl


data = """35389 6
35316 7
33921 8
1914 5
21 4
3 3
3 2
"""

values = []
dates = []

for line in data.split("\n"):
    x, y = line.split()
    values.append(int(x))
    dates.append(int(y))

fig = pl.figure()
ax = pl.subplot(111)
ax.bar(dates, values, width=100)
ax.xaxis_date()

Give this error :

File "try2.py", line 17, in x, y = line.split() ValueError: need more than 0 values to unpack

How to fix it?

0

1 Answer 1

1

I have slightly re-written some of your original code namely the string as the putting """ at the bottom like you have it will cause a redundant new element to be added to the list since you are splitting upon a new line

You have created the plot, but you still have not shown the plot I have fixed this by adding two additional lines to the bottom of your code. However, even with this you are going to have one or two more errors and I will leave these to you to figure out the resolution.

import pylab as pl

data = """35389 6
35316 7
33921 8 
1914 5
21 4
3 3
3 2"""

values = []
dates = []

for element in data.split("\n"):
    x, y= element.split()
    values.append(int(x))
    dates.append(int(y))

fig = pl.figure()
ax = pl.subplot(111)
ax.bar(dates, values, width=100)
ax.xaxis_date()
fig.show()
pl.show()
Sign up to request clarification or add additional context in comments.

3 Comments

Your right, this does have issues with it. I didn't realize i put list.split() that was a careless error and did not realize the tuple unpacking error either. Perhaps better to delete and wait for someone to post a more satisfactory answer.
The right answer is basically to either move the ending triple-quote up like you have or add a check that deals with the case where line is an empty string, if you chop your answer down to just that it'll be fine.
I think I fixed the issues. I realize what you meant now. I created to the two additonal lines when editing in my own script to compensate for the additional new line playing around with the code and forgot to revert back. That was careless of me. After that the split() is fine.

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.