难以使用子图并排显示多个方框图



我使用的是heart_failure_clinical_records_dataset.csv数据集,您可以在此处找到它:https://www.kaggle.com/abdallahwagih/heart-failure-clinical-records-dataset-eda现在,我使用以下代码创建了初始数据帧的两个子集:

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()

我希望它们中的每一个的框线图都作为输出并排显示我使用下面的代码分别绘制了它们:

sns.boxplot(x = group1['age'], y = group1['creatinine_phosphokinase'])
sns.boxplot(x = group2['age'], y = group2['creatinine_phosphokinase'])

所以我一直在四处寻找子地块和子地块2grid以及所有这些,这就是我迄今为止提出的

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)

我遇到了这个错误:

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

我不理解这个错误,所以任何帮助都将不胜感激。

我也试过这个:

plot1 = plt.subplot2grid((3, 3), (0, 0), colspan=2)
plot2 = plt.subplot2grid((3, 3), (0, 2), rowspan=3, colspan=2)
plot2.plot(x1, y1)

我得到了这个错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-89-f3ee75fcd504> in <module>
----> 1 plot2.plot('x1', 'y1')
~anaconda3libsite-packagesmatplotlibaxes_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)
~anaconda3libsite-packagesmatplotlibaxes_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):
~anaconda3libsite-packagesmatplotlibaxes_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]:
~anaconda3libsite-packagesmatplotlibaxis.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)
~anaconda3libsite-packagesmatplotlibcategory.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)
~anaconda3libsite-packagesmatplotlibaxis.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
~anaconda3libsite-packagesmatplotlibcbook__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
~anaconda3libsite-packagesmatplotlibcbook__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()
~anaconda3libsite-packagesmatplotlibcbook__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
~anaconda3libsite-packagesmatplotliblines.py in recache_always(self)
646 
647     def recache_always(self):
--> 648         self.recache(always=True)
649 
650     def recache(self, always=False):
~anaconda3libsite-packagesmatplotliblines.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
~anaconda3libsite-packagesmatplotlibcbook__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 
~anaconda3libsite-packagesnumpycore_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'

axis[0, 0]更改为axis[0]将解决此问题。

axis[0].plot(x1, y1)
axis[1].plot(x2, y2)

如果你只想并排绘制这些方框图,那么:

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()

这应该工作

最新更新