Skip to content
Advertisement

Seaborn bar plot with regression line query

I am trying to produce a bar plot with a line of regression. I am trying to follow a previous suggestion for the same problem but get an error message that I am unable to overcome. My script is as follows:

import seaborn.apionly as sns
import matplotlib.pyplot as plt
import pandas as pd

sns.set(style="white", context="score")


data = {'Days':  ['5', '10', '15', '20'],
        'Impact': ['33.7561', '30.6281', '29.5748', '29.0482']
        }

a = pd.DataFrame (data, columns = ['Days','Impact'])

print (a)

ax = sns.barplot(data=a, x=a.Days, y=a.Impact, color='lightblue' )
# put bars in background:
for c in ax.patches:
    c.set_zorder(0)
# plot regplot with numbers 0,..,len(a) as x value
sns.regplot(x=np.arange(0,len(a)), y=a.Impact, ax=ax)
sns.despine(offset=10, trim=False)
ax.set_ylabel("")
ax.set_xticklabels(['5', '10','15','20'])
plt.show()

The error message I get is:

Traceback (most recent call last):

  File "C:UsersdavidAppDataLocalProgramsSpyderpkgsIPythoncoreasync_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)

  File "C:UsersdavidAppDataLocalProgramsSpyderpkgsIPythoncoreinteractiveshell.py", line 3162, in run_cell_async
    self.displayhook.exec_result = result

  File "C:UsersdavidAppDataLocalProgramsSpyderpkgstraitletstraitlets.py", line 604, in __set__
    self.set(obj, value)

  File "C:UsersdavidAppDataLocalProgramsSpyderpkgstraitletstraitlets.py", line 578, in set
    new_value = self._validate(obj, value)

  File "C:UsersdavidAppDataLocalProgramsSpyderpkgstraitletstraitlets.py", line 610, in _validate
    value = self.validate(obj, value)

  File "C:UsersdavidAppDataLocalProgramsSpyderpkgstraitletstraitlets.py", line 1842, in validate
    if isinstance(value, self.klass):

TypeError: isinstance() arg 2 must be a type or tuple of types

ERROR! Session/line number was not unique in database. History logging moved to new session 54

but I am not sure what this means. Can anyone help?

Advertisement

Answer

Please ensure you supply int or float in the df

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

data = {'Days':  [5, 10, 15, 20],
        'Impact': [33.7561, 30.6281, 29.5748, 29.0482]
        }

a = pd.DataFrame (data, columns = ['Days','Impact'])

print (a)

ax = sns.barplot(data=a, x='Days', y='Impact', color='lightblue' )
# put bars in background:
for c in ax.patches:
    c.set_zorder(0)
# plot regplot with numbers 0,..,len(a) as x value

ax = sns.regplot(x=np.arange(0,len(a)), y=a['Impact'], marker="+")
sns.despine(offset=10, trim=False)
ax.set_ylabel("")
ax.set_xticklabels(['5', '10','15','20'])
plt.show()

enter image description here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement