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")
2
  • Combining for i,v in enumerate(xrange(nsubp)): with v=v+1 and i=i+1 seems like trouble
    – doctorlove
    Jul 1, 2013 at 14:49
  • This happens because you are using a double for loop: you are plotting all the parameters for every iteration (i.e. for every subplot).
    – sodd
    Jul 1, 2013 at 15:49

1 Answer 1

0

This happens because you have two nested for loops: the first loop iterates over each row, whereas the second loop makes the scatter plot appear on every subplot. This in turn means that every plotted parameter will appear on every subplot. To avoid this, you should avoid the double for loop.

I'm not sure I understand exactly what you want to achieve, but this should at least get you on your way.

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")
1
  • You are exactly right. My double for loops were causing the problem. When I removed the second for loop and incorporated your edits, the 5 plots only showed a single record's value. To get each record value to plot on its own, I also needed to remove the 'if row.OBJECTID' statement and dedent everything below. The script now gives me a unique plot for each record, which is what I needed. Many thanks, nordev!
    – gamarra
    Jul 1, 2013 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.