2011-07-02 4 views
4

30000 또는 7000000이 아닌 30K 또는 7M 축의 값을 인쇄하려고합니다. x < 10^6에 K (킬로) 접미사를 추가하고 x> = 10^6에 M (메가) 접미사를 추가하는 것을 의미합니다. 어떻게해야합니까?matplotlib 축의 킬로 (K) 및 메가 (M) 접미사

현재 코드 : 당신은 당신의 자신의 함수를 작성해야합니다

ax = pylab.gca() 
formatter = matplotlib.ticker.FormatStrFormatter('%.f') 
ax.xaxis.set_major_formatter(formatter) 

답변

4

는 다양한 조건에 대한 접미사를 적용 FuncFormatter 대신 StrFormatter를 사용합니다. This example에서 안내해야합니다.

7

가장 좋은 코드는 지금까지 왔어요 할 수 있습니다 :

ax = matplotlib.pyplot.gca() 
mkfunc = lambda x, pos: '%1.1fM' % (x * 1e-6) if x >= 1e6 else '%1.1fK' % (x * 1e-3) if x >= 1e3 else '%1.1f' % x 
mkformatter = matplotlib.ticker.FuncFormatter(mkfunc) 
ax.yaxis.set_major_formatter(mkformatter)