4

Background: I have some python code that I need help with parameter passing into the MakeGraph_management arcpy function. I would like to create a two-series vertical line graph for every pair of input point feature classes using the 'zip' function (ie. input fgdb1/fc1 and fgdb2/fc1 >> MakeGraph >> graph1.png; input fgdb1/fc2 and fgdb2/fc2 >> MakeGraph >> graph2.png, ...).

My Problem: Now I am trying to create graphs in batch using ArcPy/python 2.7.3, but have been struggling with the way these ArcPy functions work around the template file. I created a graph template in ArcGIS 10.2 with everything setup (axis labels, titles, footers, legend names, etc.) I just need to get the data input working to correspond with Series1 and Series2.

The code below successfully iterates and creates graphs for every pair of input datasets as planned. However, the graphs contain no plotted data. I thought I could just use MakeGraph_management and SaveGraph_management since I have a template instead of also working with the ArcGIS Graph class.

mock transect graph, no data

My Code:

# Import system modules
import arcpy
from arcpy import env

# enable overwrite outputs
arcpy.env.overwriteOutput = True

# input feature directory: yr0_transects
dir_yr0_transects = r"W:\Working\abcdefg_working\PointDistance\data\point_distance\year0\output\output_year0.gdb"

# input feature directory: yr1_transects
dir_yr1_transects = r"W:\Working\abcdefg_working\PointDistance\data\point_distance\year1\output\output_year1.gdb"

# output directory
outWorkspace = r"W:\Working\abcdefg_working\Profiles"

# Create list of year 0 Transect features
arcpy.env.workspace = dir_yr0_transects
yr0_transects_list = sorted(arcpy.ListFeatureClasses())

# Create list of year 1 Transect features
arcpy.env.workspace = dir_yr1_transects
yr1_transects_list = sorted(arcpy.ListFeatureClasses())

# Set workspace
arcpy.env.workspace = r"W:\Working\abcdefg_working\Profiles"

# Set local variables
graph_tee = r"W:\Working\abcdefg_working\Profiles\Profile_graph_template.tee"



for yr0_transect, yr1_transect in zip(yr0_transects_list, yr1_transects_list):
    try:
        #Cast to string because ListFC() returns unicode.
        string_yr0_transect = str(yr0_transect)
        string_yr1_transect = str(yr1_transect)

        # create title for graph
        graph_title = "Profiles, B.F.E." + string_yr0_transect[0:17] + string_yr0_transect[47:]


        # Create the graph object
        graph = arcpy.Graph()

        # Add a vertical bar series to the graph
        graph.addSeriesLineVertical(yr0_transect)
        graph.addSeriesLineVertical(yr1_transect)

        # Specify the title of the Graph
        graph.graphPropsGeneral.title = graph_title

        # Create graph output name
        output_graph_name = string_yr0_transect[0:17] + string_yr0_transect[47:] + "_profile" + ".png"

        # Output a vertical line graph, which is created in-memory
        arcpy.MakeGraph_management(graph_tee, graph, "temp_graph")

        #Execute SaveGraph
        mygraph = arcpy.SaveGraph_management("temp_graph", output_graph_name, "MAINTAIN_ASPECT_RATIO", "1024", "768")

        del mygraph

    except:
        print arcpy.GetMessages()
4

After an enormous amount of time debugging, I solved the problem. The fc's stored in each arcpy.ListFeatureClass() list must first be made into a feature layer (arcpy.MakeFeatureLayer) in memory before being passed to the arcpy.Graph() class, arcpy.MakeGraph, and finally arcpy.SaveGraph functions.

Word of Wisdom: Another related note for creating a graph with multiple series. This should be a common sense thing, but order matters when adding series using the arcpy.graph() class. Depending on what order you add data to an ArcGIS graph template, if you re-arrange the data series for visualization purposes before saving the template, all series must be added in the same order in your code or else your chart settings will be applied to other series. An easy way to check is to open your *.tee template file and look for the bottom section where it lists the series parameters. The *.tee file will be read top-down so the first series entry encountered (not necessarily series#1 if you rearranged in the template before saving) should be the first series you add to the graph() class, then the next one encountered, and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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