简体   繁体   English

将 numpy 数组传递给 function 给出类型错误

[英]Passing numpy array to function giving type error

Not sure why I keep getting an error when I try to plot a bar chart.不知道为什么我在尝试 plot 条形图时不断收到错误。

 def plotBar(x,y):
    plt.bar(x, y, width=1, align='center', color='plum', edgecolor='firebrick',linewidth=1)
    plt.show()

In main I am calling the function like this:我主要是这样调用 function 的:

x1=np.arange(1,101)
y1=np.arange(50,151)

classname.plotBar(x1,y1)

However, I keep getting this error:但是,我不断收到此错误:

TypeError: plotBar() takes 2 positional arguments but 3 were given

I declared your function as a static method in a class:我在 class 中将您的 function 声明为static方法:

class Xxx:
    @staticmethod
    def plotBar(x,y):
        plt.bar(x, y, width=1, align='center', color='plum',
            edgecolor='firebrick', linewidth=1)
        plt.show()

The next correction is that both arrays have to be of equal length, so I changed definition of y , so that it also has 100 elements:下一个更正是 arrays 的长度必须相等,所以我更改了y的定义,使其也有100 个元素:

x1 = np.arange(1,101)
y1 = np.arange(51,151)

(previously it had 101 elements). (以前它有101个元素)。

Then I called it:然后我叫它:

Xxx.plotBar(x1,y1)

and got a picture.并得到了一张照片。

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

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