2016-08-05 5 views
0

각 값이 다른 색상으로 표시되는 정렬 된 값 집합의 시퀀스를 플로팅하려고합니다. 예를 들어, 내 값이 같다면순차 색인 플롯 파이썬

state_seq = [2, 2, 2, 2, 0, 0, 0, 1, 3, 6, 3, 3, 3, 0, 0] 
time_seq = ['2013-05-29 09:31:00', '2013-05-29 09:46:00', '2013-07-23 12:28:00', '2013-07-23 01:53:00', '2013-08-05 02:02:00', '2013-08-05 02:08:00', '2013-08-05 04:28:00', '2013-08-06 10:20:00', '2013-08-06 03:03:00', '2013-08-06 04:13:00', '2013-08-06 04:17:00', '2013-08-06 04:36:00', '2013-08-07 11:07:00', '2013-08-07 12:28:00', '2013-08-07 12:31:00'] 

state_seq의 각 고유 요소를 색으로 표시하고 순서를 그려야합니다. 나는 더 나은 코드를 편집 - 지금, 나는,이 같은 사소한 방법으로 이루어 저에게이 같은 (이 출력이 정확히 코드와 일치하지 않을 수 있습니다

color_set = ["#95a5a6", "#34495e","#9b59b6", "#3498db", "#800000", "#2ecc71", 'y', '#FF4500'] 
palette = list() 
for s in state_seq: 
    palette.append(color_set[s])  

sns.palplot(palette) 

를 얻을 수 시본 palplot을 사용하고 있습니다 선명도) Sequence from seaborn pal plot

이 접근 방식에는 시간 레이블을 x 축으로 표시 할 수없는 장애가 있습니다. R- 패키지와 비슷한 더 나은 파이썬 대안이 있습니까? 여기에서 설명하는 TraMineR은 Is it possible to make a graph with pattern fills using TraMineR and R base graphs?입니다.

+0

이유 동등 높은 막대 '의 matplotlib' * (w/테두리 O) 및 Y 축 제거를 *의 막대 그래프를 사용하지? – jbndlr

답변

1

각 시간 간격에 관계없이 각 값의 사각형 상자를 원한다고 가정합니다. 너가 할 수있는 시간 간격을 나타내는 너비가 있으려면 convert time_seq to datetime objects 그리고 matplotlib로 날짜를 직접 플롯하십시오.

import matplotlib.pyplot as plt 
import numpy as np 

state_seq = [2, 2, 2, 2, 0, 0, 0, 1, 3, 6, 3, 3, 3, 0, 0] 
time_seq = ['2013-05-29 09:31:00', '2013-05-29 09:46:00', '2013-07-23 12:28:00', '2013-07-23 01:53:00', '2013-08-05 02:02:00', '2013-08-05 02:08:00', '2013-08-05 04:28:00', '2013-08-06 10:20:00', '2013-08-06 03:03:00', '2013-08-06 04:13:00', '2013-08-06 04:17:00', '2013-08-06 04:36:00', '2013-08-07 11:07:00', '2013-08-07 12:28:00', '2013-08-07 12:31:00'] 
color_set = ["#95a5a6", "#34495e","#9b59b6", "#3498db", "#800000", "#2ecc71", 'y', '#FF4500'] 

for i in range(len(time_seq)): 
    # fill the yaxis with our colour for each time_seq entry for width 1 
    plt.fill_between((i,i+1), 1, color=color_set[state_seq[i]]) 

# keep the coloured boxes square 
plt.axis("scaled") 

plt.xlim(0, len(time_seq)) 
plt.axes().get_yaxis().set_visible(False) 

# add custom tick labels based on time_seq and set to middle of boxes 
plt.xticks(np.arange(0.5,len(time_seq)+0.5, 1), time_seq, rotation='vertical') 
# remove xaxis ticks 
plt.axes().get_xaxis().set_ticks_position("none") 

plt.savefig("boxes.png", bbox_inches="tight") 

생산 물품 : enter image description here