简体   繁体   English

条形图上的错误(Python,Matplotlib)

[英]error on bar chart (Python, Matplotlib)

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 文件“ try2.py”,第17行,在x中,y = line.split()ValueError:需要大于0的值才能解压缩

How to fix it? 如何解决?

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM