2017-02-13 97 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!有几件事情想要改变以适应我的需求,但这些都不是这个问题的主题。 –