import matplotlib.pyplot as plt import numpy as np category_names = ['失眠', '睡眠呼吸暂停', '无'] def survey(results, category_names): """ 参数 ---------- results : 字典类型 y轴坐标标记:X轴的占比 category_names : list of str 分类标记. """ labels = list(results.keys()) data = np.array(list(results.values())) data_cum = data.cumsum(axis=1) category_colors = plt.get_cmap('RdYlGn')( np.linspace(0.15, 0.85, data.shape[1])) fig, ax = plt.subplots(figsize=(9.2, 5)) ax.invert_yaxis() ax.xaxis.set_visible(False) ax.set_xlim(0, np.sum(data, axis=1).max()) for i, (colname, color) in enumerate(zip(category_names, category_colors)): widths = data[:, i] starts = data_cum[:, i] - widths ax.barh(labels, widths, left=starts, height=0.5, label=colname, color=color) xcenters = starts + widths / 2 r, g, b, _ = color text_color = 'white' if r * g * b < 0.5 else 'darkgrey' for y, (x, c) in enumerate(zip(xcenters, widths)): ax.text(x, y, str(int(c)), ha='center', va='center', color=text_color) ax.legend(ncol=len(category_names), bbox_to_anchor=(0, 1), loc='lower left', fontsize='small') return fig, ax survey(results, category_names) survey(results1, category_names) survey(results2, category_names) survey(results3, category_names) survey(results4, category_names)怎么将五个图形一起展示
时间: 2023-12-15 07:06:52 浏览: 127
Matplotlib.pyplot 三维绘图的实现示例
您可以使用Matplotlib的子图(subplot)功能将这五个图形显示在同一个图像中。您可以使用如下代码:
```
fig, axs = plt.subplots(nrows=1, ncols=5, figsize=(20, 5))
survey(results, category_names, ax=axs[0])
survey(results1, category_names, ax=axs[1])
survey(results2, category_names, ax=axs[2])
survey(results3, category_names, ax=axs[3])
survey(results4, category_names, ax=axs[4])
plt.show()
```
这将创建一个1行5列的图像,每个子图都是通过调用`survey`函数来创建的。`ax`参数用于指定子图的位置。最后,使用`plt.show()`来显示图像。
阅读全文