Search code examples
pythonlistbar-chartseabornattributeerror

How to fix AttributeError when using barplot in python?


I'm trying to get a barplot out of some data in python through using the library seaborn. My data looks something like this:

data_list = [[value_1, value_2, value_3, value_4], [1, 2, 3, 4]]

I'm now trying to execute the following command:

ax = sns.barplot(x = 'x_name', y = 'y_name', data = data_list)

Unfortunately, instead of getting a barplot, I get the following line:

File "C:\Users\ (my name) \AppData\Local\Programs\Python\Python36-32\lib\site-packages\seaborn\categorical.py", line 146, in establish_variables            
x = data.get(x, x)  
AttributeError: 'list' object has no attribute 'get'

How can I fix this error? Do I need to provide the data in a different format?


Solution

  • The function documentation specifies that data optional argument needs to be DataFrame, array, or list of arrays, whereas yours is a list of lists. But you can also just pass the individual data arrays directly to the positional arguments x and y:

    ax = sns.barplot(data_list[0], data_list[1])
    

    Should do the trick.