I am using the heart_failure_clinical_records_dataset.csv dataset, you can find it here: https://www.kaggle.com/abdallahwagih/heart-failure-clinical-records-dataset-eda Now I created two subsets of the initial dataframe using the code below:
group1 = a[(a["sex"] == 1) & (a["diabetes"] == 1) & (a["high_blood_pressure"] == 0)]group1.head()
group2 = a[(a["sex"] == 1) & (a["diabetes"] == 1) & (a["high_blood_pressure"] == 1)]group2.head()
I want the resulting boxplots for each of them to show up side by side as an output I have plotted them individually by using the code below:
sns.boxplot(x = group1['age'], y = group1['creatinine_phosphokinase'])
sns.boxplot(x = group2['age'], y = group2['creatinine_phosphokinase'])
So I have been going around looking at subplots and subplot2grid and all that and this is what I have come up with so far
x1 = group1['age']
y1 = group1['creatinine_phosphokinase']
x2 = group2['age']
y2 = group2['creatinine_phosphokinase']
figure, axis = plt.subplots(1, 2)
axis[0, 0].plot(x1, y1)
I get hit with this error:
IndexError Traceback (most recent call last)
<ipython-input-86-126627fbbb24> in <module>
----> 1 axis[0, 0].plot(x1, y1)
2 #axis[1, 0].plot(x2, y2)
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
I do not understand this error so any help will be appreciated.
I have also tried this:
plot1 = plt.subplot2grid((3, 3), (0, 0), colspan=2)
plot2 = plt.subplot2grid((3, 3), (0, 2), rowspan=3, colspan=2)
plot2.plot(x1, y1)
And I get this error:
TypeError Traceback (most recent call last)
<ipython-input-89-f3ee75fcd504> in <module>
----> 1 plot2.plot('x1', 'y1')
~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
1741 """
1742 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1743 lines = [*self._get_lines(*args, data=data, **kwargs)]
1744 for line in lines:
1745 self.add_line(line)
~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in __call__(self, data, *args, **kwargs)
271 this = args[0],
272 args = args[1:]
--> 273 yield from self._plot_args(this, kwargs)
274
275 def get_next_color(self):
~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
394 self.axes.xaxis.update_units(x)
395 if self.axes.yaxis is not None:
--> 396 self.axes.yaxis.update_units(y)
397
398 if x.shape[0] != y.shape[0]:
~\anaconda3\lib\site-packages\matplotlib\axis.py in update_units(self, data)
1464 neednew = self.converter != converter
1465 self.converter = converter
-> 1466 default = self.converter.default_units(data, self)
1467 if default is not None and self.units is None:
1468 self.set_units(default)
~\anaconda3\lib\site-packages\matplotlib\category.py in default_units(data, axis)
105 # the conversion call stack is default_units -> axis_info -> convert
106 if axis.units is None:
--> 107 axis.set_units(UnitData(data))
108 else:
109 axis.units.update(data)
~\anaconda3\lib\site-packages\matplotlib\axis.py in set_units(self, u)
1539 self.units = u
1540 self._update_axisinfo()
-> 1541 self.callbacks.process('units')
1542 self.callbacks.process('units finalize')
1543 self.stale = True
~\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in process(self, s, *args, **kwargs)
227 except Exception as exc:
228 if self.exception_handler is not None:
--> 229 self.exception_handler(exc)
230 else:
231 raise
~\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in _exception_printer(exc)
79 def _exception_printer(exc):
80 if _get_running_interactive_framework() in ["headless", None]:
---> 81 raise exc
82 else:
83 traceback.print_exc()
~\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in process(self, s, *args, **kwargs)
222 if func is not None:
223 try:
--> 224 func(*args, **kwargs)
225 # this does not capture KeyboardInterrupt, SystemExit,
226 # and GeneratorExit
~\anaconda3\lib\site-packages\matplotlib\lines.py in recache_always(self)
646
647 def recache_always(self):
--> 648 self.recache(always=True)
649
650 def recache(self, always=False):
~\anaconda3\lib\site-packages\matplotlib\lines.py in recache(self, always)
651 if always or self._invalidx:
652 xconv = self.convert_xunits(self._xorig)
--> 653 x = _to_unmasked_float_array(xconv).ravel()
654 else:
655 x = self._x
~\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in _to_unmasked_float_array(x)
1287 return np.ma.asarray(x, float).filled(np.nan)
1288 else:
-> 1289 return np.asarray(x, float)
1290
1291
~\anaconda3\lib\site-packages\numpy\core\_asarray.py in asarray(a, dtype, order, like)
100 return _asarray_with_like(a, dtype=dtype, order=order, like=like)
101
--> 102 return array(a, dtype, copy=False, order=order)
103
104
TypeError: float() argument must be a string or a number, not 'pandas._libs.interval.Interval'
CodePudding user response:
Changing axis[0, 0]
to axis[0]
will fix the issue.
axis[0].plot(x1, y1)
axis[1].plot(x2, y2)
If you only want to plot those boxplots side by side then:
fig, ax =plt.subplots(1, 2, figsize=(25, 8))
sns.boxplot(x = group1['age'], y = group1['creatinine_phosphokinase'], ax=ax[0])
sns.boxplot(x = group2['age'], y = group2['creatinine_phosphokinase'], ax=ax[1])
fig.show()
this should work