0

I need to create a summary plot of a single parameter for each record in my database. Using the code below, I have managed to create a subplot for each record (5 in test database, ArcGIS 10.0 file geodatabase, Python 2.6.5, Matplotlib 1.0.0), but each subplot is identical. I have searched through the forum examples of summary plots/reports, subplot syntax, and looping techniques in my attempt to identify the proper syntax. I expect that my issue is an improper loop syntax, since I am plotting all records per plot instead of the desired one record per plot. After I resolve this basic plotting issues, I plan to expand the scope of my code to include 10-15 parameters per plot, 3-4 plots total, and some general summary information, all on a single page pdf per record. I am working with a few thousand records in total.

This is my first post on Stack Overflow. The forum has been an extremely helpful resource to me on numerous occasions over the past year. I am new to python and brand-new to using matplotlib, but I see the enormous potential of the language and this library. Any help or suggestions are much appreciated!

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")
4

1 回答 1

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")
于 2013-07-01T16:16:33.977 回答