2009-12-17 3 views
1

나는 'matplotlib'을 사용하기 시작했고 2 개의 주요 장애물에 부딪혔다. 나는 docs/examples 등에서이 문제를 해결할 수 없다. 여기는 파이썬 소스이다 :matplotlib 초보자 부부의 의문점

#!/usr/bin/python 
import matplotlib 
matplotlib.use('Agg') 

import matplotlib.pyplot as plt 
for i in range(0,301): 

    print "Plotting",i 

    # Reading a single column data file 
    l=plt.plotfile("gen"+str(i)) 

    plt.xlabel('Population') 
    plt.ylabel('Function Value') 
    plt.title('Generation'+str(i)) 
    plt.axis([0,500,0,180]) 

    plt.plot() 

    if len(str(i)) == 1: 
     plt.savefig("../images/plot00"+str(i)+".png") 
    if len(str(i)) == 2: 
     plt.savefig("../images/plot0"+str(i)+".png") 
    if len(str(i)) == 3: 
     plt.savefig("../images/plot"+str(i)+".png") 

    plt.clf() 
  1. 의심 1 : 당신이 볼 수 있듯이을, 나는 기본적으로 플롯을 취소하고 다음 새 플롯마다 시간을 절약. 나는 Y 축의 범위를 일정하게 유지하기를 원하며 "plt.axis ([0,500,0,180])"를 통해이를 수행하려고합니다. 하지만 작동하지 않는 것처럼 보이며 매번 자동으로 설정됩니다.
  2. 의심 2 : 포인트가 연속 선으로 결합 된 기본 플롯을 얻는 대신 '*'라는 플롯을 얻는 것이 좋습니다. 내가 어떻게 그럴 수 있니?
+2

질문과 관련,하지만 당신은 문자열 형식을 사용하여 IFS를 제거 할 수 없음 :' "../images/plot"+str(i).zfill(3)+".png을 "또는 더 나은 (파이썬 2.6 이상)"../ images/plot {0 : 03d} .png ".format (i)' –

+0

덕분에 톤 팀 :) –

+0

... 또는"% 03d "% i ... – EOL

답변

2

  • 팀 Pietzcker가 지적 하듯이 에 의해 마지막에 파일 이름 코드 문자열 숫자 서식을 사용하는 경우, 당신은 단축 할 수 있습니다.

    filename='plot%03d.png'%i 
    

    i 최대 3 제로의 패딩 정수와 %03d을 대체합니다. Python2.6 +에서 , 하나는 덜 예쁜하지만 더 강력하고 새로운 문자열 서식 구문과 같은 일을 할 수 :

    filename='plot{0:03d}.png'.format(i) 
    

  • 별 플롯 포인트를 얻으려면, 당신은을 사용할 수 있습니다 옵션 marker='*'. 그리고 연결선을 제거하려면 linestyle='none'을 사용하십시오.
  • plt.plotfile (...)은 그림을 그립니다. plt.plot()에 대한 호출은 첫 번째 그림 위에 두 번째 그림이 겹쳐서 표시됩니다. plt.plot()을 호출하면 축 치수가 수정되어 plt.axis(...)의 효과가 사라집니다. 다행히도 수정 방법은 간단합니다. plt.plot()을 호출하지 마십시오. 너는 필요 없어.

#!/usr/bin/env python 
import matplotlib 
import matplotlib.pyplot as plt 

matplotlib.use('Agg') # This can also be set in ~/.matplotlib/matplotlibrc 
for i in range(0,3): 
    print 'Plotting',i 
    # Reading a single column data file 
    plt.plotfile('gen%s'%i,linestyle='none', marker='*') 

    plt.xlabel('Population') 
    plt.ylabel('Function Value') 
    plt.title('Generation%s'%i) 
    plt.axis([0,500,0,180]) 
    # This (old-style string formatting) also works, especial for Python versions <2.6: 
    # filename='plot%03d.png'%i 
    filename='plot{0:03d}.png'.format(i) 
    print(filename) 
    plt.savefig(filename) 
    # plt.clf() # clear current figure 
+0

쿨! 엄청 고마워. 나는 그림 클래스 등으로 무언가를 할 필요가 있을지 모른다는 것을 두려워했다. –