2017-10-15 4 views
0

저는 현재 1990 년에서 2014 년 (25 행)까지의 인덱스를 가진 데이터 프레임을 가지고 있습니다. 제 음모가 모든 해를 보여주는 X 축을 갖기를 바랍니다. 이 그림에 4 개의 플롯을 가질 계획이므로 add_subplot을 사용하고 있습니다 (모두 동일한 X 축을 사용합니다).Xticks 주파수를 데이터 프레임 인덱스로 설정

import pandas as pd 
import numpy as np 

index = np.arange(1990,2015,1) 
columns = ['Total Population','Urban Population'] 

pop_plot = pd.DataFrame(index=index, columns=columns) 
pop_plot = df_.fillna(0) 

pop_plot['Total Population'] = np.arange(150,175,1) 
pop_plot['Urban Population'] = np.arange(50,125,3) 

내가 현재 가지고있는 코드 : 이것은 내가 할 그 플롯이다

fig = plt.figure(figsize=(10,5)) 
ax1 = fig.add_subplot(2,2,1, xticklabels=pop_plot.index) 
plt.subplot(2, 2, 1) 

plt.plot(pop_plot) 
legend = plt.legend(pop_plot, bbox_to_anchor=(0.1, 1, 0.8, .45), loc=3, ncol=1, mode='expand') 
legend.get_frame().set_alpha(0) 

ax1.set_xticks(range(len(pop_plot.index))) 

:

는 dataframe를 만들려면

Plot with ax1.set_xticks

내가 코멘트 set_xticks 다음 플롯을 얻습니다.

#ax1.set_xticks(range(len(pop_plot.index))) 

Regular plot

은 내가 여기 답변의 몇 가지를 시도했다, 그러나 나는 많은 성공을하지 않았다.

미리 감사드립니다.

+0

왜 당신이 기대는'범위 (LEN은())'1900에서 시작 값을 줄 것이다? – roganjosh

+0

나는 그렇지 않다. 레이블은 xticklabels = pop_plot.index에 의해 제공됩니다. – vgastaldi

+0

나는 그 질문을 이해할 수 있을지 확신하지 못한다. 나는 당신의 응답에 따라'ax1.set_xticks (range (len (pop_plot.index))'의 의도 된 기능을 전혀 이해하지 못했다. 또한 데이터를 보지 않으면 왜 얻지 못하는지 알 수가 없다. – roganjosh

답변

0

무엇이 ax1.set_xticks(range(len(pop_plot.index)))에 사용해야하는지 확실하지 않습니다.

ax1.set_xticks(pop_plot.index) 
: 플롯은 1990 년부터 2014 년

범위해야하면서는 대신, 데이터의 숫자에 틱을 설정하려는 숫자 0, 1, 2, 3 등으로 진드기를 설정합니다

전체 수정 예 :

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

index = np.arange(1990,2015,1) 
columns = ['Total Population','Urban Population'] 

pop_plot = pd.DataFrame(index=index, columns=columns) 

pop_plot['Total Population'] = np.arange(150,175,1) 
pop_plot['Urban Population'] = np.arange(50,125,3) 


fig = plt.figure(figsize=(10,5)) 
ax1 = fig.add_subplot(2,2,1) 

ax1.plot(pop_plot) 
legend = ax1.legend(pop_plot, bbox_to_anchor=(0.1, 1, 0.8, .45), loc=3, ncol=1, mode='expand') 
legend.get_frame().set_alpha(0) 

ax1.set_xticks(pop_plot.index) 
plt.show() 
+0

내가 어떻게 생각 났는지는 모르겠지만 답을 사용하여 완벽하게 작동했습니다. 감사합니다. – vgastaldi

관련 문제