سؤال

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")
هل كانت مفيدة؟

المحلول

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")
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top