Show a graph with python

7558
5
03-22-2015 09:16 PM
Warwick
New Contributor II

Using the GP tools and python it is possible to dynamically create a graph in ArcMap but how can I display the graph to the user?

0 Kudos
5 Replies
NeilAyres
MVP Alum

Regardless of how the graph is made, either manually or via a script, I think you still have to open the graph in ArcMap.

View / Graph and then pick it.

If you are creating the graph using arcpy / python, is this happening within an open ArcMap mxd?

0 Kudos
JamesCrandall
MVP Frequent Contributor

Yes it is possible.  It's probably not as straight-forward as you might be expecting though.  For our implementation(s), we employ the matplotlib library to generate our graphs, save as .png and then populate File Geodatabase(s) as attachments to their related FeatureClasses.

I couldn't really begin to post up any code examples unless you have something specific in mind. Post up an example of your data.

Warwick
New Contributor II

Workflow would be as follows. User has an ArcMap document open in which a NetCDF Raster layer is visible. The user clicks on a location to view how a variable changes over time or levels. All the steps can be scripted using python, GP tools and a graph template except for the final step of displaying the graph to the user. Am I missing something?

0 Kudos
JamesCrandall
MVP Frequent Contributor

Here's a short example using matplotlib (although there are probably far better examples found with a quick Google search or on Stack Overflow).

Sample data:

H:\GraphData.csv
    Month  Values
0       1  2.0228
1       2  2.0229
2       3  2.0231
3       4  2.0232
4       5  2.0233
5       6  2.0234
6       7  2.0235
7       8  2.0236
8       9  2.0237
9      10  2.0237
10     11  2.0238
11     12  2.0239
12     13  2.0240
13     14  2.0241
14     15  2.0243
15     16  2.0244
16     17  2.0247
17     18  2.0247
18     19  2.0248
19     20  2.0249
20     21  2.0250
21     22  2.0250
22     23  2.0252
23     24  2.0253

py Code:

import pandas as pd
from pandas import *
import matplotlib.pyplot as plt

data = r'H:\GraphData.csv'
df = pd.io.parsers.read_table(data, sep=',')
xs = df.Month
ys = df['Values']

min = df['Month'].min()
max = df['Month'].max()

fig = plt.figure()
ax = "ax" + str(211)
ax = fig.add_subplot(211, axisbg='white')
ax.plot(xs, ys, linestyle='-', marker='', linewidth=1, color='blue', label=str('Graph Test'))

plt.savefig(r'H:\GraphData.png', dpi=600)
plt.close()  
del fig
XanderBakker
Esri Esteemed Contributor

I posted a document a while ago with an example of creating a profile graph with matplotlib:

Using Python and matplotlib to create profile graphs

Dan Patterson​ has some valuable posts on this topic too:

Collections in numpy: producing frequency distributions and graphing​and X-Y graphing demo using pyplot (matplotlib)...an introduction