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:
JavaScript
x
26
26
1
import seaborn.apionly as sns
2
import matplotlib.pyplot as plt
3
import pandas as pd
4
5
sns.set(style="white", context="score")
6
7
8
data = {'Days': ['5', '10', '15', '20'],
9
'Impact': ['33.7561', '30.6281', '29.5748', '29.0482']
10
}
11
12
a = pd.DataFrame (data, columns = ['Days','Impact'])
13
14
print (a)
15
16
ax = sns.barplot(data=a, x=a.Days, y=a.Impact, color='lightblue' )
17
# put bars in background:
18
for c in ax.patches:
19
c.set_zorder(0)
20
# plot regplot with numbers 0,..,len(a) as x value
21
sns.regplot(x=np.arange(0,len(a)), y=a.Impact, ax=ax)
22
sns.despine(offset=10, trim=False)
23
ax.set_ylabel("")
24
ax.set_xticklabels(['5', '10','15','20'])
25
plt.show()
26
The error message I get is:
JavaScript
1
24
24
1
Traceback (most recent call last):
2
3
File "C:UsersdavidAppDataLocalProgramsSpyderpkgsIPythoncoreasync_helpers.py", line 68, in _pseudo_sync_runner
4
coro.send(None)
5
6
File "C:UsersdavidAppDataLocalProgramsSpyderpkgsIPythoncoreinteractiveshell.py", line 3162, in run_cell_async
7
self.displayhook.exec_result = result
8
9
File "C:UsersdavidAppDataLocalProgramsSpyderpkgstraitletstraitlets.py", line 604, in __set__
10
self.set(obj, value)
11
12
File "C:UsersdavidAppDataLocalProgramsSpyderpkgstraitletstraitlets.py", line 578, in set
13
new_value = self._validate(obj, value)
14
15
File "C:UsersdavidAppDataLocalProgramsSpyderpkgstraitletstraitlets.py", line 610, in _validate
16
value = self.validate(obj, value)
17
18
File "C:UsersdavidAppDataLocalProgramsSpyderpkgstraitletstraitlets.py", line 1842, in validate
19
if isinstance(value, self.klass):
20
21
TypeError: isinstance() arg 2 must be a type or tuple of types
22
23
ERROR! Session/line number was not unique in database. History logging moved to new session 54
24
but I am not sure what this means. Can anyone help?
Advertisement
Answer
Please ensure you supply int
or float
in the df
JavaScript
1
25
25
1
import seaborn as sns
2
import matplotlib.pyplot as plt
3
import pandas as pd
4
import numpy as np
5
6
data = {'Days': [5, 10, 15, 20],
7
'Impact': [33.7561, 30.6281, 29.5748, 29.0482]
8
}
9
10
a = pd.DataFrame (data, columns = ['Days','Impact'])
11
12
print (a)
13
14
ax = sns.barplot(data=a, x='Days', y='Impact', color='lightblue' )
15
# put bars in background:
16
for c in ax.patches:
17
c.set_zorder(0)
18
# plot regplot with numbers 0,..,len(a) as x value
19
20
ax = sns.regplot(x=np.arange(0,len(a)), y=a['Impact'], marker="+")
21
sns.despine(offset=10, trim=False)
22
ax.set_ylabel("")
23
ax.set_xticklabels(['5', '10','15','20'])
24
plt.show()
25