2017-02-13 1 views
1

thisthis의 질문을 최선을 다합니다. 즉, 테스트 이름, 실행 날짜 및 결과가 포함 된 DataFrame이 있습니다. 그리고 나는 실패한 사례의 비율이 시간이 지남에 따라 어떻게 감소하는지 보여주고 싶습니다.X 축에 날짜가있는 matplotlib의 100 % 스택 영역/막대 그래프

내 데이터는 다음과 같습니다

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 (데이터 프레임에 많은 항목이 있으므로 for 루프가없는)
  3. 2 단계의 금액이 100 % 영역 차트 또는 각 버킷의 누적 막대 그래프 중 하나를 플롯하여 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 축의 절대 개수에서 백분율로 전환하는 방법이 있습니까? 현재 일부 쓰레기통은 1000 회 이상 실행되지만 나머지는 100 미만입니다 ... –

+0

편집 된 답변보기 그래도 도움이되지 않으면 언제든지 문의하십시오. – ImportanceOfBeingErnest

+0

이것은 내가 찾고 있었던 바로 그 것이다! 고마워, 영원히! 내 요구에 맞게 변경하고 싶은 몇 가지 사항이 있지만이 내용은 주제와 관련이 없습니다. –

관련 문제