<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Creating serial graphs with arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368668#M29054</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This is the code I tried to export graphs with but I get the same graph for every selected feature:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy,os
path = os.getcwd() 
pageCount = 1
mxd = arcpy.mapping.MapDocument(path + r"\test_graph_exp.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "Counties_sel")[0]
list_of_attributes = [row.getValue('Name') for row in arcpy.SearchCursor(lyr.dataSource)]
 
for bez in list_of_attributes:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Make a graph for each line of the attribute table
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; whereClause = "\"name\" = '" + bez + "'"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = whereClause
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION", whereClause)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SaveGraph_management("test_graph", (path + r"\graph" + str(pageCount) + ".bmp") , "MAINTAIN_ASPECT_RATIO", "600", "375")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pageCount = pageCount + 1
del mxd&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 17:05:33 GMT</pubDate>
    <dc:creator>StefanSchneider</dc:creator>
    <dc:date>2021-12-11T17:05:33Z</dc:date>
    <item>
      <title>Creating serial graphs with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368665#M29051</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I need to make export reports with graphs. One map and one graph for each selected feature in a loop. When I add the graph to the layout and set the graph so that it only shows the values of the selected features it doesn't update when a new feature is selected with arcpy. Also setting the graph to show all values and making a query that only the selected feature is shown, doesn't work. Still the graph doesn't update. Embedding the graph to the layout of the map would be my favourite. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;My second unsuccessful try was to select features and export the resulting graph with SaveGraph_management, but here I experienced the same problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;When I select features manuelly/write a definition query manually the graph updates. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I would be really happy if you could help me, I've been trying to solve this problem for quite a few hours.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Pedol&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Apr 2013 19:21:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368665#M29051</guid>
      <dc:creator>StefanSchneider</dc:creator>
      <dc:date>2013-04-03T19:21:20Z</dc:date>
    </item>
    <item>
      <title>Re: Creating serial graphs with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368666#M29052</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How are you creating the graph? i have successfully done this with arcpy and matplotlib for data driven pages.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Apr 2013 20:20:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368666#M29052</guid>
      <dc:creator>CarlSunderman</dc:creator>
      <dc:date>2013-04-03T20:20:03Z</dc:date>
    </item>
    <item>
      <title>Re: Creating serial graphs with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368667#M29053</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;How are you creating the graph? i have successfully done this with arcpy and matplotlib for data driven pages.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have created graphs with ArcGis (something like View --&amp;gt; Graphs --&amp;gt; Create Graph)&amp;nbsp; also in the Python window in ArcGIS without matplotlib, similiar to the sample script here &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018z0000005t000000"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#//018z0000005t000000&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Apr 2013 20:53:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368667#M29053</guid>
      <dc:creator>StefanSchneider</dc:creator>
      <dc:date>2013-04-03T20:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: Creating serial graphs with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368668#M29054</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This is the code I tried to export graphs with but I get the same graph for every selected feature:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy,os
path = os.getcwd() 
pageCount = 1
mxd = arcpy.mapping.MapDocument(path + r"\test_graph_exp.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "Counties_sel")[0]
list_of_attributes = [row.getValue('Name') for row in arcpy.SearchCursor(lyr.dataSource)]
 
for bez in list_of_attributes:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Make a graph for each line of the attribute table
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; whereClause = "\"name\" = '" + bez + "'"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.definitionQuery = whereClause
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION", whereClause)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SaveGraph_management("test_graph", (path + r"\graph" + str(pageCount) + ".bmp") , "MAINTAIN_ASPECT_RATIO", "600", "375")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pageCount = pageCount + 1
del mxd&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:05:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368668#M29054</guid>
      <dc:creator>StefanSchneider</dc:creator>
      <dc:date>2021-12-11T17:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: Creating serial graphs with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368669#M29055</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;In a new mxd the code works. The problem seems to have been with the mxd. Thank you very much for trying to help me.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 06 Apr 2013 18:00:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368669#M29055</guid>
      <dc:creator>StefanSchneider</dc:creator>
      <dc:date>2013-04-06T18:00:30Z</dc:date>
    </item>
    <item>
      <title>Re: Creating serial graphs with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368670#M29056</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Carl,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am trying to do exactly this but am struggling slightly.&amp;nbsp; Would you mind sharing the code with me at all please?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Many thanks in advance&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 May 2015 15:03:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368670#M29056</guid>
      <dc:creator>MichelleConvey</dc:creator>
      <dc:date>2015-05-06T15:03:58Z</dc:date>
    </item>
    <item>
      <title>Re: Creating serial graphs with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368671#M29057</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well, are you struggling slightly(almost there, just something in your code is not right) or are you lost (nothing works like it should) &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&amp;nbsp; Perhaps i can help you if you have something that just isn't quite working, if you are just lost,or not sure how to go about this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My script uses numpy, matplotlib, and scipy, so you would need to install those modules in python (not sure if numpy is installed with arcpy, but i think it is.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What my script does:&lt;/P&gt;&lt;P&gt;1. So you have a point feature class with a field that matches the DDP sheet number that it falls in&lt;/P&gt;&lt;P&gt;2. It then selects those features, and creates a graph, for that page generalizing for missing data to get rid of the jagged edges, as a png in the output folder&lt;/P&gt;&lt;P&gt;3. Then it inserts the graph onto the page and exports, and moves on to the next.&lt;/P&gt;&lt;P&gt;So at the end, in the output folder there are the pdf's with the graph inserted on them and all of the png's in case you want them for something else.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I really only use this as a function now to create the graphs, I have other functions in my tools that inserts graphs, graphics, pictures, labels, etc on the map. I tried to comment it to make it easier to understand, but let me know if you have any questions.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy,os,glob
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import spline

mxd = arcpy.mapping.MapDocument(r"&amp;lt;MXD file with Data Driven Pages&amp;gt;")
outputFolder = "C:/&amp;lt;ouput location&amp;gt;/"
# there should be a picture element on your page at the location where you want the graph inserted
pictureElementName = "&amp;lt;Picture Element Name&amp;gt;"
eleFieldFeature = "&amp;lt;Elevation Point Feature Class&amp;gt;"
eleField = "&amp;lt;Elevation Field&amp;gt;"
# distField can be whatever field you want for the X axis
distField = "&amp;lt;Distance Field&amp;gt;"

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
pict = arcpy.mapping.ListLayoutElements(mxd, "PICTURE_ELEMENT", pictureElementName)[0]
lyr = arcpy.mapping.ListLayers(mxd, eleFieldFeature, df)[0]


for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.dataDrivenPages.currentPageID = pageNum
&amp;nbsp;&amp;nbsp;&amp;nbsp; # change this SeqId to match the field name that you have in the DDP featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp; # this is redundant, but it works all the same and I didn't feel like changing it. :)
&amp;nbsp;&amp;nbsp;&amp;nbsp; pageName = mxd.dataDrivenPages.pageRow.getValue("SeqId")
&amp;nbsp;&amp;nbsp;&amp;nbsp; # change this SeqId to match the field name that you have in the Elevation featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", ' "SeqId" = '+ repr(pageName))
&amp;nbsp;&amp;nbsp;&amp;nbsp; # create array of selected
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr = arcpy.da.FeatureClassToNumPyArray(lyr, (eleField, distField))
&amp;nbsp;&amp;nbsp;&amp;nbsp; # set min and max for the graph from the array 
&amp;nbsp;&amp;nbsp;&amp;nbsp; mineleField = arr[eleField].min()
&amp;nbsp;&amp;nbsp;&amp;nbsp; maxeleField = arr[eleField].max()
&amp;nbsp;&amp;nbsp;&amp;nbsp; minStation = arr[distField].min()
&amp;nbsp;&amp;nbsp;&amp;nbsp; maxStation = arr[distField].max()
&amp;nbsp;&amp;nbsp;&amp;nbsp; # matplotlib stuff
&amp;nbsp;&amp;nbsp;&amp;nbsp; x = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; y = []

&amp;nbsp;&amp;nbsp;&amp;nbsp; elevPNG = outputFolder+"\elev"+repr(pageNum)+".png"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fig = plt.figure(figsize=(32, 2.8))
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; table = lyr
&amp;nbsp;&amp;nbsp;&amp;nbsp; fields = [distField, eleField]
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(table, fields) as rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x.append(row[0])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; y.append(row[1])


&amp;nbsp;&amp;nbsp;&amp;nbsp; # the next 7 lines generalize, smooth the line in case of missing data
&amp;nbsp;&amp;nbsp;&amp;nbsp; x_sm = np.array(x)
&amp;nbsp;&amp;nbsp;&amp;nbsp; y_sm = np.array(y)
&amp;nbsp;&amp;nbsp;&amp;nbsp; x_smooth = np.linspace(x_sm.min(), x_sm.max(),200)
&amp;nbsp;&amp;nbsp;&amp;nbsp; y_smooth = spline(x,y,x_smooth)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # subtract/add 10 to the min max of the graph so elevation fits, can increase or decrease as you need
&amp;nbsp;&amp;nbsp;&amp;nbsp; plt.ylim((mineleField-10,maxeleField+10))
&amp;nbsp;&amp;nbsp;&amp;nbsp; plt.xlim((minStation,maxStation))
&amp;nbsp;&amp;nbsp;&amp;nbsp; plt.plot(x_smooth,y_smooth, color='red', linewidth=3)


&amp;nbsp;&amp;nbsp;&amp;nbsp; ## the next 3 is your X label, Y Label, and Graph Title
&amp;nbsp;&amp;nbsp;&amp;nbsp; plt.xlabel('Distance')
&amp;nbsp;&amp;nbsp;&amp;nbsp; plt.ylabel('Elevation')
&amp;nbsp;&amp;nbsp;&amp;nbsp; plt.title('Landscape Profile')
&amp;nbsp;&amp;nbsp;&amp;nbsp; plt.grid(True)
&amp;nbsp;&amp;nbsp;&amp;nbsp; fig.savefig(elevPNG,&amp;nbsp; bbox_inches='tight', dpi=(100))
&amp;nbsp;&amp;nbsp;&amp;nbsp; pict.sourceImage = elevPNG
&amp;nbsp;&amp;nbsp;&amp;nbsp; # end of matplotlib stuff 


&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.ExportToPDF(mxd, outputFolder + str(pageName) + "_W_GRAPH.pdf", resolution=150, image_quality="NORMAL")
&amp;nbsp;&amp;nbsp;&amp;nbsp; plt.clf()
&amp;nbsp;&amp;nbsp;&amp;nbsp; plt.close()
&amp;nbsp;&amp;nbsp;&amp;nbsp; del fig
del mxd&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:05:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368671#M29057</guid>
      <dc:creator>CarlSunderman</dc:creator>
      <dc:date>2021-12-11T17:05:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating serial graphs with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368672#M29058</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Carl!&amp;nbsp; This helped immensely and I've now changed the chart to a bar chart which is what I needed for my output!&amp;nbsp; Thanks so much for your prompt reply and such detailed comments - much appreciated!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 May 2015 15:13:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368672#M29058</guid>
      <dc:creator>MichelleConvey</dc:creator>
      <dc:date>2015-05-07T15:13:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating serial graphs with arcpy</title>
      <link>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368673#M29059</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for this, Carl.&lt;BR /&gt;&lt;BR /&gt;This script was very helpful. I just adapted it for getting the elevation and distance data directly from a polyline FeatureClass with Z and M coordinates and it worked like a charm. Thanks again.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Sep 2017 15:11:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-serial-graphs-with-arcpy/m-p/368673#M29059</guid>
      <dc:creator>CarlosSousa_Ferreira</dc:creator>
      <dc:date>2017-09-26T15:11:22Z</dc:date>
    </item>
  </channel>
</rss>

