2017-09-09 13 views
0

버스 정류장과 같은 두 지점 간의 관계를 보여주는 데이터 세트를 다루고 있습니다. 예를 들어, 버스 정류장 A, B, C 및 D가 있습니다.python - seaborn : 공유 X 레이블이 예상대로 작동하지 않습니다.

각 버스 정류장마다 다른 3 개의 버스 정류장까지가는 데 걸리는 시간을 보여주는 히스토그램 플롯을 만듭니다.

분명히 A부터 A까지 시간이 없기 때문에 비어 있어야합니다.

내가 그릴 때 첫 번째 행에 B C D가 표시되고 두 번째 행에 A, C, D 등이 표시되는 것을 볼 수 있습니다. 열의 정렬이 잘못되어 색이 각 행의 같은 열을 나타내지는 않습니다.

sharex = True를 추가하면 각 축의 x 레이블이 제거됩니다. 분명히 여기서보고 싶은 것이 아닙니다.

대신 A, B, C, D 순으로 4 개의 열을보고 싶습니다. A에서 A 일 때는 공백이어야하며 색상은 일관성이 있어야합니다.

누구든지 이것을 수행하는 방법을 알고 있습니까? 문자열 목록,
의 범주 수준을 음모 주문 옵션, 그렇지 않으면 수준 :

import pandas as pd 
import numpy as np 
import seaborn as sns 
%matplotlib inline 

time=np.random.randn(1000) 
point1 = ['A','B','C','D'] * 250 
point2 = ['A'] * 250 + ['B'] * 250 + ['C'] * 250 + ['D'] * 250 

df_time = pd.DataFrame(
    {'point1': point1, 
    'point2': point2, 
    'time': time 
    }) 
df_time=df_time[df_time['point1']!=df_time['point2']] ##cannot sell to another 

fig, ax = plt.subplots(nrows=4, sharey=True) 
fig.set_size_inches(12, 16) 
for point1i, axi in zip(point1, ax.ravel()): 
    sns.boxplot(data=df_time[df_time['point1']==point1i], x='point2', y='time', ax=axi) 

the documentation에서 볼 수 있듯이 enter image description here

답변

1

, sns.boxplot는 argumen order

order, hue_order있다 데이터 객체로부터 추론됩니다. 당신이 원하는 플롯을 줄 것 같은

sns.boxplot(..., order=['A','B','C','D']) 

를 사용

.

전체 코드 :

import pandas as pd 
import numpy as np 
import seaborn as sns 
import matplotlib.pyplot as plt 

time=np.random.randn(1000) 
point1 = ['A','B','C','D'] * 250 
point2 = ['A'] * 250 + ['B'] * 250 + ['C'] * 250 + ['D'] * 250 

df_time = pd.DataFrame(
    {'point1': point1, 
    'point2': point2, 
    'time': time 
    }) 
df_time=df_time[df_time['point1']!=df_time['point2']] ##cannot sell to another 

fig, ax = plt.subplots(nrows=4, sharey=True) 

for point1i, axi in zip(point1, ax.ravel()): 
    sns.boxplot(data=df_time[df_time['point1']==point1i], x='point2', y='time', 
       ax=axi, order=['A','B','C','D']) 

plt.tight_layout()  
plt.show() 

enter image description here