AttributeError 'tuple' object has no attribute 'get' using Seaborn to bar plot

Question:

I have an application that’s been giving me some trouble for a bit of time.

This is the trace I’m being given:

File "C:UsersuserPycharmProjectspythonProjectvenvlibsite-packagesseaborncategorical.py", line 532, in establish_variables
    x = data.get(x, x)
AttributeError: 'tuple' object has no attribute 'get'

I’m trying to graph information that is being generated and saved using PyIBL, the information from that function is being returned like this:

  def runExperiment(trials, participants):
    """
    code that does the work/not relevant
    """
    return np.array(experimentTotals).mean(axis=0), np.vstack(experimentResults)

I’m trying to create the bar graph here:

def plotResults(totals):
    labels = ["Successful", "Failed", "Withdrawls"]
    sns.barplot(x='experimentTotals', y='experimentResults', data=totals)

Where totals is the output being returned from runExperiment, my question is how can I modify what is being returned in runExperiment in order for it to be displayed with plotResults? Or am I just using Seaborn incorrectly?

Asked By: Floresss

||

Answers:

You need to create a DataFrame or an array-like object with named variables from the output of runExperiment and pass that to barplot. Here’s an example of how you can modify runExperiment to return a DataFrame:

import pandas as pd

def runExperiment(trials, participants):
    """
    code that does the work/not relevant
    """
    experimentTotals = np.array(experimentTotals).mean(axis=0)
    experimentResults = np.vstack(experimentResults)
    df = pd.DataFrame({'experimentTotals': experimentTotals,
                       'Successful': experimentResults[:, 0],
                       'Failed': experimentResults[:, 1],
                       'Withdrawals': experimentResults[:, 2]})
    return df

This will create a DataFrame with columns named experimentTotals, Successful, Failed, and Withdrawals. You can then pass this DataFrame to barplot:

def plotResults(totals):
    sns.barplot(x='experimentTotals', y='value', hue='variable', data=pd.melt(totals, ['experimentTotals']))
Answered By: PermanentPon

So i belive a dict like object is supposed to go in X, not a tuple, and also for other X you are passing a string experimentTotals, when its supposed to be actual data

anyways, based on the code you provided i think you are trying to plot the mean values of the experimentTotals array against the rows of the experimentResults array.

To do this, you can modify your runExperiment() function to return a dictionary-like object with keys ‘successful’, ‘failed’, and ‘withdrawls’, each containing a list of mean values for that category like so:

def runExperiment(trials, participants):
    """
    code that does the work/not relevant
    """
    experimentTotals = np.array(experimentTotals)
    means = experimentTotals.mean(axis=0)
    return {'successful': [means[0]], 'failed': [means[1]], 'withdrawls': [means[2]]}, np.vstack(experimentResults)

Then, in your plotResults() function, you can pass the dict object returned by runExperiment as the param, and specify the appropriate keys for the x and y parameters. heres a exmaple:

def plotResults(totals):
    labels = ["Successful", "Failed", "Withdrawls"]
    sns.barplot(x=labels, y='successful', data=totals)
    sns.barplot(x=labels, y='failed', data=totals)
    sns.barplot(x=labels, y='withdrawls', data=totals)

let me know if there are any questions

Answered By: Mohit Reddy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.