简体   繁体   English

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

[英]Matplotlib grouped data clustered bar chart in Python

I have a dictionary of values ( drug ) as follows:我有一个值字典( 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}}

It contains 3 drug types, then the efficacy of that drug type in 4 different concentrations.它包含 3 种药物类型,然后是该药物类型在 4 种不同浓度下的功效。

I'm trying to make a clustered bar chart which compares the 3 drugs against each other like this:我正在尝试制作一个聚类条形图,将 3 种药物相互比较,如下所示:

在此处输入图片说明

Currently, my code is the following:目前,我的代码如下:

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

I have adapted the code from this guide, but am having multiple problems.我已经改编了 指南中的代码,但遇到了多个问题。

I think the main reason is that the data used in the example is such that values in one group correspond to the same colour rather than the same cluster.我认为主要原因是示例中使用的数据使得一组中的值对应于相同的颜色而不是相同的集群。

How can I adapt this code such that it will plot the efficacy of each drug in the 4 different concentrations in isolation compared to the other drugs?我如何调整此代码,以便与其他药物相比,它可以单独绘制每种药物在 4 种不同浓度下的功效?

IIUC you want to normalize your values by column, which can be done using sklearn : 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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM