2013-07-01 37 views
0

我需要爲我的數據庫中的每個記錄創建單個參數的摘要圖。使用下面的代碼,我設法爲每條記錄創建一個子圖(測試數據庫中的5個,ArcGIS 10.0文件地理數據庫,Python 2.6.5,Matplotlib 1.0.0),但每個子圖都是相同的。我通過論壇搜索了總結圖/報告的示例,子圖句法和循環技術,以便確定正確的語法。我期望我的問題是一個不正確的循環語法,因爲我繪製每個繪圖的所有記錄而不是每個繪圖所需的一個記錄。在我解決這些基本的繪圖問題之後,我計劃擴大我的代碼範圍,使每個繪圖包含10-15個參數,總計3-4個繪圖,以及一些一般摘要信息,全部在每個記錄的單個頁面上。我正在處理總共幾千條記錄。matplotlib每個記錄的總結圖

這是我在Stack Overflow上的第一篇文章。在過去的一年裏,論壇對我來說是非常有用的資源。我是python的新手,也是使用matplotlib的全新人選,但我看到了這門語言和這個庫的巨大潛力。任何幫助或建議非常感謝!

import arcpy 
import os 
import matplotlib 
import matplotlib.pyplot as plt 

#Variables 
FC = arcpy.GetParameterAsText(0) #feature class 
P1_fld = arcpy.GetParameterAsText(1) #score field to chart 
P2_fld = arcpy.GetParameterAsText(2) #score field to chart 
plt.subplots_adjust(hspace=0.4) 
nsubp = int(arcpy.GetCount_management(FC).getOutput(0)) #pulls n subplots from FC 
last_val = object() 

#Sub-plot loop 
cur = arcpy.SearchCursor(FC, "", "", P1_fld) 
for row in cur: 
    x=1 
    y=row.getValue(P1_fld) 
    if row.OBJECTID != last_val: 
     for i,v in enumerate(xrange(nsubp)): 
      v = v+1 
      i = i+1 
      ax = plt.subplot(nsubp,1,v) # Create a subplot. 
      ax.scatter(x,y,s=5,color='blue'); # Generate the Scatter Plot. 
      oid = str(row.getValue('OBJECTID')) 
      figPDf = r"filepath.pdf" # Save the Scatter Plot to PDF. 
      plt.savefig(figPDf) 
del row, cur 
os.startfile("filepath.pdf") 
+0

對我結合,V在枚舉(x範圍(nsubp)):與V = V + 1和i = i + 1似乎是麻煩 – doctorlove

+0

這是因爲您使用的是雙'for'循環:你繪製每次迭代的所有參數(即每個子圖)。 – hooy

回答

0

這是因爲你有兩個嵌套的循環for:第一循環遍歷每個row,而第二循環使得散點圖上出現插曲。這又意味着每個繪圖參數將出現在每個子圖上。爲避免這種情況,應避免使用雙重for循環。

我不確定我確切地理解你想達到的目標,但是這至少應該讓你走上正軌。

import arcpy 
import os 
import matplotlib 
import matplotlib.pyplot as plt 

#Variables 
FC = arcpy.GetParameterAsText(0) #feature class 
P1_fld = arcpy.GetParameterAsText(1) #score field to chart 
P2_fld = arcpy.GetParameterAsText(2) #score field to chart 
plt.subplots_adjust(hspace=0.4) 
nsubp = int(arcpy.GetCount_management(FC).getOutput(0)) #pulls n subplots from FC 
last_val = object() 

#Sub-plot loop 
cur = arcpy.SearchCursor(FC, "", "", P1_fld) 
i = 0 
x = 1 
for row in cur: 
    y = row.getValue(P1_fld) 
    if row.OBJECTID != last_val: 
     i += 1 
     ax = plt.subplot(nsubp, 1, i) # Create a subplot. 
     ax.scatter(x, y, s=5, color='blue'); # Generate the Scatter Plot. 
     oid = str(row.getValue('OBJECTID')) 
     figPDf = r"filepath.pdf" # Save the Scatter Plot to PDF. 
     plt.savefig(figPDf) 
del row, cur 
os.startfile("filepath.pdf") 
+0

你完全正確。我的雙循環導致了這個問題。當我刪除第二個循環併合並編輯時,5個圖只顯示了單個記錄的值。爲了讓每個記錄值自行繪圖,我還需要刪除'if row.OBJECTID'語句並縮小下面的所有內容。劇本現在給我一個獨特的每個記錄的情節,這是我所需要的。非常感謝,nordev! – gamarra