I'm trying to make a function for a data set. The function basically takes one of the feature names as a string, and then the function is supposed to return an x axis value and a y axis value which will be used to make a bar chart. The bar chart is supposed to show the correlation between a desired feature (in this case the sepal-length) and another fixed feature (in this case the 'species' classification).
This is what a sample of the data set looks like: enter image description here
def correlation_group(feature):
y_axis = df[['species',feature]].groupby('species').mean()
x_axis= df.species.unique()
y = []
for i in range(1) :
y.append(y_axis[i:])
print(y)
return x_axis, y
y = 'sepal_length'
x,y = correlation_group(y)
print(y)
plt.bar(x,y)
The result I get after running this is the error:
TypeError Traceback (most recent call last)
<ipython-input-39-cd8e1ad8a982> in <module>
12 x,y = correlation_group(y)
13 print(y)
---> 14 plt.bar(x,y)
D:\anaconda3\lib\site-packages\matplotlib\pyplot.py in bar(x, height, width, bottom, align, data, **kwargs)
2485 x, height, width=0.8, bottom=None, *, align='center',
2486 data=None, **kwargs):
-> 2487 return gca().bar(
2488 x, height, width=width, bottom=bottom, align=align,
2489 **({"data": data} if data is not None else {}), **kwargs)
D:\anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1445 def inner(ax, *args, data=None, **kwargs):
1446 if data is None:
-> 1447 return func(ax, *map(sanitize_sequence, args), **kwargs)
1448
1449 bound = new_sig.bind(ax, *args, **kwargs)
D:\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in bar(self, x, height, width, bottom, align, **kwargs)
2479 args = zip(left, bottom, width, height, color, edgecolor, linewidth)
2480 for l, b, w, h, c, e, lw in args:
-> 2481 r = mpatches.Rectangle(
2482 xy=(l, b), width=w, height=h,
2483 facecolor=c,
D:\anaconda3\lib\site-packages\matplotlib\patches.py in __init__(self, xy, width, height, angle, **kwargs)
740 """
741
--> 742 Patch.__init__(self, **kwargs)
743
744 self._x0 = xy[0]
D:\anaconda3\lib\site-packages\matplotlib\patches.py in __init__(self, edgecolor, facecolor, color, linewidth, linestyle, antialiased, hatch, fill, capstyle, joinstyle, **kwargs)
86 self.set_fill(fill)
87 self.set_linestyle(linestyle)
---> 88 self.set_linewidth(linewidth)
89 self.set_antialiased(antialiased)
90 self.set_hatch(hatch)
D:\anaconda3\lib\site-packages\matplotlib\patches.py in set_linewidth(self, w)
391 w = mpl.rcParams['axes.linewidth']
392
--> 393 self._linewidth = float(w)
394 # scale the dash pattern by the linewidth
395 offset, ls = self._us_dashes
TypeError: only size-1 arrays can be converted to Python scalars