2013-07-01 56 views
0

我無法使用Matplotlib在循環中繪製多個系列(Matplotlib 1.0.0,Python 2.6.5,ArcGIS 10.0)。論壇研究指出我應用Axes對象,以便在同一個圖上繪製多個系列。我看到這對於在循環之外生成的數據(示例腳本)是如何工作的,但是當我插入相同的語法並將第二個系列添加到從數據庫中提取數據的循環中時,出現以下錯誤:使用循環中的軸對象的matplotlib散點圖

「 :不支持的操作數類型爲 - :'NoneType'和'NoneType'無法執行(ChartAge8)。「

以下是我的代碼 - 任何建議或意見非常感謝!

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 
x1 = 1 # category 1 locator along x-axis 
x2 = 2 # category 2 locator along x-axis 
fig = plt.figure() 
for row in cur: 
    y1 = row.getValue(P1_fld) 
    y2 = row.getValue(P2_fld) 
    i += 1 
    ax1 = fig.add_subplot(nsubp, 1, i) 
    ax1.scatter(x1, y1, s=10, c='b', marker="s") 
    ax1.scatter(x2, y2, s=10, c='r', marker="o") 
del row, cur 

#Save plot to pdf, open 
figPDf = r"path.pdf" 
plt.savefig(figPDf) 
os.startfile("path.pdf") 

回答

0

如果你想要做的是劇情的幾個東西,重複使用相同的情節,你應該做的是建立循環外圖對象,然後繪製到同一個對象每次,像這樣:

fig = plt.figure() 

for row in cur: 
    y1 = row.getValue(P1_fld) 
    y2 = row.getValue(P2_fld) 
    i += 1 

    ax1 = fig.add_subplot(nsubp, 1, i) 
    ax1.scatter(x1, y1, s=10, c='b', marker="s") 
    ax1.scatter(x2, y2, s=10, c='r', marker="o") 
del row, cur 
+0

感謝您的反饋。我用你的建議修改了腳本,但是我仍然像以前一樣得到相同的錯誤。 – gamarra

+0

@gamarra你還在用循環的每次迭代創建一個新的圖形對象 –

+0

Paul,我編輯了上面的代碼以反映腳本中的更改。據我所知,圖形對象現在只創建一次,但是每個記錄都會重複出現子圖對象,這就是目標。但是,我只能在循環內添加一個單獨的子圖對象(對於系列2,最終我需要添加12個系列)。只要我添加第二個子對象,如上面的代碼所示,我得到了NoneType錯誤。任何想法如何解決這個問題? – gamarra