1 回答

TA貢獻1801條經(jīng)驗 獲得超8個贊
我不確定這是否是您要查找的內(nèi)容,但是如果每列都應(yīng)該是條形,并且您希望每列中的時間增量,那么您需要每行之間的天數(shù)差異,我是猜測第一行應(yīng)該有0天的差異(因為它是起點)。
同樣對于堆疊條形圖,索引用于創(chuàng)建類別,但在您的情況下,您希望列作為類別,并且每個條形圖由不同的索引值組成。這意味著您最終需要轉(zhuǎn)置您的 df 。
這個解決方案非常丑陋,但希望它有所幫助。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
"Initiative_160608": ['2009-05-15', '2009-05-22', '2009-05-28', '2009-06-17', '2009-06-17'],
"Initiative_160570": ['2009-05-15', '2009-05-22', '2009-05-28', '2009-06-17', '2009-06-17'],
"Initiative_160056": ['2006-04-07', '2006-04-26', '2006-05-11', '2006-05-17', '2006-05-26']})
df.index = ['Beschless_BR', 'Vorlage_BT', 'Beratung_BT', 'ABeschless_BT', 'Beschlussempf']
# convert everything to dates
df = df.apply(lambda x: pd.to_datetime(x, format="%Y-%m-%d"))
def get_days(x):
diff_list = []
for i in range(len(x)):
if i == 0:
diff_list.append(x[i] - x[i])
else:
diff_list.append(x[i] - x[i-1])
return diff_list
# get the difference in days, then convert back to numbers
df_diff = df.apply(lambda x: get_days(x), axis = 0)
df_diff = df_diff.apply(lambda x: x.dt.days)
# transpose the matrix so that each initiative becomes a stacked bar
df_diff = df_diff.transpose()
# replace 0 values with 0.2 so that the bars are visible
df_diff = df_diff.replace(0, 0.2)
df_diff.plot.bar(stacked = True)
plt.show()
添加回答
舉報