I made a 3D line plot Python add-in with matplotlib for ArcMap.
After I execute this tool 3-4 times, ArcMap crashed. I have also executed it in the Python window, but the situation is just the same.
Here is the code:
import arcpy
import pythonaddins
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
class ButtonClass1(object):
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
# plot line
def plotWireframe(x,y,z,c):
ax.plot_wireframe(x,y,z,color=c)
mxd = arcpy.mapping.MapDocument("CURRENT")
arcpy.mapping.ListLayers(mxd)
layers = arcpy.mapping.ListLayers(mxd)
select_layers = [str(i.name) for i in layers if arcpy.Describe(i.name).fidSet]
numOfLayer = 0
for fc in select_layers:
if numOfLayer%3 == 0:
color = (0.1, 0.2, 0.5)
elif numOfLayer%2 == 0:
color = (0.5, 0.8, 0.5)
else:
color = (0.6, 0.2, 0.1)
numOfLayer = numOfLayer + 1
desc = arcpy.Describe(fc)
geometryType = desc.shapeType
if geometryType == 'Polyline':
for row in arcpy.da.SearchCursor(fc, ["SHAPE@"]):
for part in row[0]:
x = []
y = []
z = []
for pnt in part:
x.append(pnt.X)
y.append(pnt.Y)
if pnt.Z == None:
z.append(0)
else:
z.append(pnt.Z)
plotWireframe(x,y,z,color)
else:
print fc, "is not Polyline"
plt.show()
I guess the problem might caused by:
- Memory problem: but it happened even I only select one feature (polyline) to plot.
- The python GUI may not play well with ArcMap
The version information is as follows:
- ArcMap 10.2, 10.5 and 10.5.1 (both got the same crash)
- matplotlib version 1.5.2 (a build-in module in ArcGIS)
Has anyone using matplotlib in ArcMap encountered a similar problem?

name 'numOfLayer' is not defined.plt.show(), no crashing.with arcpy.da.SearchCursor() as cursor:syntax instead (sample code in its help).