2017-02-13 70 views
1

我想充分利用thisthis問題。也就是說,我有一個DataFrame,其中包含測試名稱,執行日期和結果。我想展示失敗案例的百分比隨着時間的推移而下降。matplotlib中的100%堆積面積/直方圖,日期在X軸上

我的數據是這樣的:

TestName;Date;IsPassed 
test1;12/8/2016 9:44:30 PM;0 
test1;12/8/2016 9:39:00 PM;0 
test1;12/8/2016 9:38:29 PM;1 
test1;12/8/2016 9:38:27 PM;1 
test2;12/8/2016 5:05:02 AM;1 
test3;12/7/2016 8:58:36 PM;0 
test3;12/7/2016 8:57:19 PM;1 
test3;12/7/2016 8:56:15 PM;1 
test4;12/5/2016 6:50:49 PM;0 
test4;12/5/2016 6:49:50 PM;0 
test4;12/5/2016 3:23:09 AM;1 
test4;12/4/2016 11:51:29 PM;1 

而我使用此代碼分別繪製的情況:

fig, ax = plt.subplots() 
passed = tests[tests.IsPassed == 1] 
failed = tests[tests.IsPassed == 0] 
passed_dates = mdates.date2num(passed.Date.astype(datetime)) 
failed_dates = mdates.date2num(failed.Date.astype(datetime)) 
ax.hist(passed_dates, bins=10, color='g') 
ax.hist(failed_dates, bins=10, color='r') 
ax.xaxis.set_major_locator(mdates.AutoDateLocator()) 
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%y')) 
plt.show() 

但現在我想

  1. 鴻溝時間間隔分配到可配置數量的桶中
  2. 計算t est每桶運行(不包含循環,因爲數據框中有很多條目)
  3. 繪製100%面積圖或每個桶的堆疊直方圖,以使步驟2中的數量爲100%

對我來說現在的問題是,隨着hist()的完美工作液照顧總結的自動,我不明白的方式,以Y軸傳遞給它。

更新

這裏是想什麼我來完成(從其他來源獲得): 100% Stacked columns

回答

1

使用參數stacked = True允許你提供幾個陣列作爲輸入plt.hist

ax.hist([passed_dates, failed_dates], bins=10, stacked=True, label=["passed", "failed"]) 

enter image description here

使用相對計數需要通過每個倉的絕對計數的數量來劃分。該功能不是內置於hist函數中的。您需要手動計算直方圖,然後將結果繪製爲堆積條形圖。

from __future__ import division 
import matplotlib.pyplot as plt 
import matplotlib.dates 
import datetime 
import numpy as np 
import pandas as pd 

dates = pd.date_range("2016/01/01","2016/06/01") 
dates2 = pd.date_range("2016/02/01","2016/03/17", freq="18H") 
dates = dates.append(dates2) 

passed = np.round(np.random.rand(len(dates))+0.231).astype(np.int8) 
tests = pd.DataFrame({"Date" : dates, "IsPassed": passed}) 

fig, ax = plt.subplots() 
passed = tests[tests.IsPassed == 1] 
failed = tests[tests.IsPassed == 0] 
all_dates = matplotlib.dates.date2num(tests.Date.astype(datetime.datetime)) 
passed_dates = matplotlib.dates.date2num(passed.Date.astype(datetime.datetime)) 
failed_dates = matplotlib.dates.date2num(failed.Date.astype(datetime.datetime)) 

hist, bins = np.histogram(all_dates, bins=10) 
histpassed, bins_ = np.histogram(passed_dates, bins=bins) 
histfailed, bins__ = np.histogram(failed_dates, bins=bins) 

binwidth=bins[1]-bins[0] 
ax.bar(bins[:-1]+binwidth/2., histpassed/hist, width=binwidth*0.8, label="passed") 
ax.bar(bins[:-1]+binwidth/2., histfailed/hist, width=binwidth*0.8, bottom=histpassed/hist, label="failed") 

ax.xaxis.set_major_locator(matplotlib.dates.AutoDateLocator()) 
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d.%m.%y')) 
ax.legend() 
fig.autofmt_xdate() 
plt.savefig(__file__+".png") 
plt.show() 

enter image description here

+0

不錯!感謝提示。現在有辦法從Y軸的絕對計數切換到百分比嗎?現在有些垃圾箱總計超過一千次,而另一些垃圾箱則少於100 ... –

+0

查看編輯答案。如果這仍然沒有幫助,請隨時進一步詢問。 – ImportanceOfBeingErnest

+0

這正是我一直在尋找的!謝謝,@ImportanceOfBeingErnest!有幾件事情想要改變以適應我的需求,但這些都不是這個問題的主題。 –