【问题标题】:TypeError: bar() got multiple values for keyword argument 'height'TypeError: bar() 为关键字参数“高度”获取了多个值
【发布时间】:2017-06-27 16:27:05
【问题描述】:

我尝试使用 python 重新创建我的一个 excel 图表,但现在不断撞墙:

这是我设法去的代码:

import matplotlib.pyplot as plt
from numpy import arange

myfile = open(r'C:\Users\user\Desktop\Work In Prog\Alpha Data.csv', 'r')

label = [] # this is a string of the label
data = []  #this is some integer, some are the same value

for lines in myfile:

    x = lines.split(',')
    label.append(x[1])
    data.append(x[4])

dataMin = float(min(data))
dataMax = float(max(data))

pos = arange(dataMin, dataMax, 1)

p1 = plt.bar(pos, data, color='red', height=1)

plt.show()

【问题讨论】:

    标签: python python-2.7 bar-chart


    【解决方案1】:

    bar 需要以下内容:

    matplotlib.pyplot.bar(left, height, width=0.8, bottom=None, hold=None, data=None, **kwargs)
    

    你在做:

    p1 = plt.bar(pos, data, color='red', height=1)
    

    由于您(错误地)将data 作为第二个位置参数传递,所以当您将height 作为命名参数传递时,它已经被传递了。

    快速修复:

    p1 = plt.bar(pos, 1, color='red', data=data)
    

    (我承认我没有检查您的数据是否合规)

    【讨论】:

    • 太棒了!你用这个拯救了我的一天。
    • 我已经完成了一半,按照正确的顺序只是仍然将其视为关键字参数,不得不将其更改为常规参数,因此,从 width=widthwidth
    猜你喜欢
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    相关资源
    最近更新 更多