Python中的Matplotlib分组数据聚集条形图

2024-09-29 19:35:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个值字典(drug),如下所示:

{0: {0: 100.0, 1: 0.41249706379061035, 2: 5.144449764434768, 3: 31.078456871927678}, 1: {0: 100.0, 1: 0.6688801420346955, 2: 77.32360971119694, 3: 78.15132480853421}, 2: {0: 100.0, 1: 136.01949766418852, 2: 163.4967732211563, 3: 146.7726208999281}}

它包含3种药物类型,然后该药物类型在4种不同浓度下的疗效

我试图制作一个聚集条形图,将3种药物相互比较如下:

enter image description here

目前,我的代码如下:

fig, ax = plt.subplots()
width = 0.35
ind = np.arange(3)

for x in range(3):
    ax.bar(ind + (width * x), drug[x].values(), width, bottom=0)

ax.set_title('Drug efficacy')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(list(string.ascii_uppercase[0:drugCount]))

ax.autoscale_view()

plt.show()

我已经修改了this指南中的代码,但是有多个问题

我认为主要原因是示例中使用的数据是一组中的值对应于相同的颜色,而不是相同的簇

我如何调整该代码,使其能够绘制出与其他药物相比,每种药物在4种不同浓度下的疗效


Tags: 代码类型字典figpltaxwidth条形图
1条回答
网友
1楼 · 发布于 2024-09-29 19:35:03

IIUC您想按列规范化您的值,这可以使用sklearn来完成:

from sklearn import preprocessing

df = pd.DataFrame(drug)
scaler = preprocessing.MinMaxScaler()
df = pd.DataFrame(scaler.fit_transform(df))
df.T.plot(kind="bar")
plt.show()

enter image description here

相关问题 更多 >

    热门问题