2014-01-15 3 views
1

범례에 레이블을 추가하는 데 문제가 있습니다. 어떤 이유로 matplotlib은 데이터 프레임에서 만든 라벨을 무시합니다. 어떤 도움이 필요합니까?플롯의 레이블

팬더 버전 : 0.13.0
하기 matplotlib 버전 : 1.3.1

import pandas as pd 
%matplotlib inline 
import matplotlib.pyplot as plt 

# Sample dataframe 
d = {'date': [pd.to_datetime('1/1/2013'), pd.to_datetime('1/1/2014'), pd.to_datetime('1/1/2015')], 
    'number': [1,2,3], 
    'letter': ['A','B','C']} 
df = pd.DataFrame(d) 

#################### 
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(13, 10)) 
fig.subplots_adjust(hspace=2.0) ## Create space between plots 

# Chart 1 
df.plot(ax=axes[0], label='one') 

# Chart 2 
df.set_index('date')['number'].plot(ax=axes[1], label='two') 

# add a little sugar 
axes[0].set_title('This is the title') 
axes[0].set_ylabel('the y axis') 
axes[0].set_xlabel('the x axis') 
axes[0].legend(loc='best') 
axes[1].legend(loc='best'); 

문제 차트 1 "번호"와 같은 전설을 반환하는 것입니다 그리고 그것이 "하나"를 말하고 싶습니다.

답변

1

첫 번째 축에 대해 설명합니다. 두 번째 반복 할 수 있습니다.

In [72]: fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(13, 10)) 

범례의 현재 텍스트를 인쇄 범례

In [75]: text = legend.get_texts()[0] 

의 텍스트를 가져 오기 범례

In [74]: legend = ax.get_legend() 

을 축

In [73]: ax=df.plot(ax=axes[0]) 

참조 얻기

In [77]: text.get_text() 
Out[77]: u'number' 

드로잉

In [79]: plt.draw() 

다음 그림을 업데이트 원하는 텍스트

In [78]: text.set_text("one") 

설정은 제 1 축에 대한 변경된 범례를 도시한다. 다른 축에 대해서도 동일한 작업을 수행 할 수 있습니다.

NB : IPython 자동 완성 기능이이 대답을 이해하는 데 많은 도움이되었습니다!

enter image description here

+0

덕분에, 나는 또한 [0] .legend ([ "라벨 차트 1", LOC = '최고'),하지만 난 아직 잘 모르겠어요 왜 내 원래의 코드 축을 사용하여 작동하도록 있어요 작동하지 않습니다. – DataByDavid