2017-05-17 3 views
1

긴 레이블이있는 데이터 프레임을 표시하려고합니다. 플롯은 주로 그래프로 표시되어 레이블을 표시하려고합니다. 내가 가진 :Matplotlib + Pandas = 라벨 보는 법?

new_labels = [] 
for i, index in enumerate(df.index): 
    new_label = "%s (%.2f)"%(index,df.performance[i]) 
    new_labels.append(new_label) 

fig , axes = plt.subplots(1,1) 
df.sort_values(col_name).plot(kind='barh', ax=axes) 
axes.xaxis.set_ticklabels(new_labels) 
axes.xaxis.set_major_locator(ticker.MultipleLocator(1)) 

나에게 제공 : 당신이 볼 수 있듯이 enter image description here

, 라벨이 표시되지 않습니다. 실제로이 값은 다음과 같습니다

new_labels 
['PLS regression\n\n PLSRe (0.12)', 
'Regression based on k-nea (0.44)', 
'The Gaussian Process mode (0.46)', 
'Orthogonal Matching Pursu (0.52)', 
'An extremely randomized t (0.54)', 
'RANSAC (RANdom SAmple Con (0.56)', 
'Elastic Net model with it (0.66)', 
'Kernel ridge regression. (0.67)', 
'Cross-validated Orthogona (0.67)', 
'Linear Model trained with (0.68)', 
'Linear regression with co (0.68)', 
'Theil-Sen Estimator (0.68)', 
'Lasso linear model with i (0.69)', 
'Bayesian ridge regression (0.70)'... 

어떻게 내 레이블에 더 많은 공간을 제공하고, 짧은 막대가 할 수 있습니까?

+0

가 왜'figsize'을 증가시키지? 'plt.figure (그림 크기 = (10, 10))' – spies006

답변

0

당신은 레이블이 완전히 표시 할 수있는 기회가되도록 플롯의 왼쪽에 공간을 더 추가 할 수 있습니다. 이것은 0.5는도 폭의 50 %를 의미

fig.subplots_adjust(left=0.5) 

를 사용하여 수행 될 수있다.

전체 예제 :

import pandas as pd 
import matplotlib.pyplot as plt 

labels = ['PLS regression PLSRe (0.12)', 
'Regression based on k-nea (0.44)', 
'The Gaussian Process mode (0.46)', 
'Orthogonal Matching Pursu (0.52)', 
'An extremely randomized t (0.54)', 
'RANSAC (RANdom SAmple Con (0.56)', 
'Elastic Net model with it (0.66)', 
'Kernel ridge regression. (0.67)', 
'Cross-validated Orthogona (0.67)', 
'Linear Model trained with (0.68)', 
'Linear regression with co (0.68)', 
'Theil-Sen Estimator (0.68)', 
'Lasso linear model with i (0.69)', 
'Bayesian ridge regression (0.70)'] 

values = [0.12, .44,.46,.52,.54,.56,.66,.67,.67,.68,.68,.68,.69,.7] 

df = pd.DataFrame(values, index=labels) 

fig , axes = plt.subplots(1,1) 
fig.subplots_adjust(left=0.5) 
df.plot(kind='barh', ax=axes) 

plt.show() 

enter image description here

0

이 시도 :

df.sort_values(col_name).plot(x=new_labels, kind='barh', ax=axes) 
# NOTE:      ^^^^^^^^^^^^ 
관련 문제