使用Pandas对齐堆叠条形图

2024-09-30 08:15:36 发布

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

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

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

谢谢你!你知道吗


Tags: columnsdfcurrentax条形图palettexanthomyco99
1条回答
网友
1楼 · 发布于 2024-09-30 08:15:36

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

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

根据我们在评论中的讨论,我认为这是我们所期望的。嗯,告诉我这是不是你要找的!你知道吗

相关问题 更多 >

    热门问题