对齐堆积的条形图usind熊猫

时间:2018-08-22 18:11:09

标签: python pandas stacked

我正在尝试对齐所有具有相同索引的堆叠条形图。 最好的方法是什么?

stacked_bar_plots

到目前为止,这是我的代码:

xantho99 = [545/60, 6/60, 1688/60, 44/60]
buch99 = [51/60, 2/60, 576/60, 7/60]
myco99 = [519/60, 9/60, 889/60, 28/60]
cory99 = [247/60, 5/60, 1160/60, 28/60]
xantho90 = [545/60, 8/60, 989/60, 27/60]
buch90 = [51/60, 3/60, 523/60, 5/60]
myco90 = [519/60, 11/60, 802/60, 32/60]
cory90 = [247/60, 7/60, 899/60, 27/60]
xanthouc = [545/60, 0/60, 5407/60, 193/60]
buchuc = [51/60, 0/60, 1014/60, 20/60]
mycouc = [519/60, 0/60, 4644/60, 101/60]
coryuc = [247/60, 0/60, 2384/60, 77/60]
df = pd.DataFrame([xantho, xantho99, xantho90, buch, buch99, buch90, myco, myco99, myco90, cory, cory99, cory90], columns=['Prodigal', 'Cd-hit', 'PSOT', 'Zusammenführen'], index=["X", "X","X", "B", "B","B", "M", "M","M", "C", "C","C"])
df.columns.name = "Abschnitt"
current_palette = "blue", "green", "red", "yellow"
ax = df.plot.bar(stacked=True, title="Zeitbedarf der einzelnen Abschnitte (Xanthomonas)", xlim=(0, sum(xantho)*1.1), color=current_palette, rot=0)
ax.set_xlabel("Zeit in Stunden")

谢谢!

1 个答案:

答案 0 :(得分:0)

以下是相关代码(如果将其放在问题脚本的底部,应该可以使用):

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
...
...
...
# Comment out old plot
# ax = df.plot.bar(stacked=True, title="Zeitbedarf der einzelnen Abschnitte (Xanthomonas)", xlim=(0, sum(xantho)*1.1), color=current_palette, rot=0)                                                         
# ax.set_xlabel("Zeit in Stunden")                                                                                                                   
spacing = [0, 0, 0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0]
p1 = plt.bar(np.arange(12) + spacing, df['Prodigal'], color="blue", width=1.0, edgecolor="black")
p2 = plt.bar(np.arange(12) + spacing, df['PSOT'], bottom=df['Prodigal'], color="red", width=1.0, edgecolor="black")
p3 = plt.bar(np.arange(12) + spacing, df['Zusammenführen'], bottom=df['Prodigal'] + df['PSOT'], color="yellow", width=1.0, edgecolor="black")
p4 = plt.bar(np.arange(12) + spacing, df['Cd-hit'], bottom=df['Prodigal'] + df['PSOT'] + df['Zusammenführen'], color="green", width=1.0, edgecolor="black")
plt.legend((p1[0], p2[0], p3[0], p4[0]), ('Prodigal', 'PSOT', 'Zusammenführen', 'Cd-hit'))
plt.show()

这是运行此代码段后弹出的内容:

Plot output

根据我们在评论中的讨论,我认为这是所需要的。 HTH,让我知道这是否是您想要的!

相关问题