1

Not sure why I keep getting an error when I try to plot a bar chart.

 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:

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

2
  • 3
    You need to declare plotBar as: def plotBar(self,x,y): since it seems to be a class method of classname Commented May 25, 2020 at 13:13
  • 1
    classname.plotBar(x1,y1) It seems like you've tried to create a class that has a method called plotBar. When creating a method, the first variable (often called self) is the class instance. You can try to change plotBar(x,y) into plotBar(self,x,y). Commented May 25, 2020 at 13:13

1 Answer 1

1

I declared your function as a static method in a class:

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:

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

(previously it had 101 elements).

Then I called it:

Xxx.plotBar(x1,y1)

and got a picture.

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

Comments

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.