AnsweredAssumed Answered

Arc Toolbox Python script tool plot legend error

Question asked by james.sturman on Nov 20, 2017
Latest reply on Nov 20, 2017 by Dan_Patterson

I have a Python script with a optional function that when called creates terrain profile plots. When I run the script outside of ArcGIS it works fine, but when I package it up as a script tool in the Arc Toolbox to share with others it crashes with the following error message...


File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\", line 3552, in legend
ret = gca().legend(*args, **kwargs)
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\axes\", line 550, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\", line 385, in __init__
self._init_legend_box(handles, labels, markerfirst)
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\", line 654, in _init_legend_box
fontsize, handlebox))
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\", line 119, in legend_artist
fontsize, handlebox.get_transform())
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\", line 193, in create_artists
legline = Line2D(xdata, ydata)
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\", line 286, in __init__
TypeError: expected string or buffer


I have tried many variations of the legend call in the script, but nothing has worked so far. I could get around this by not plotting a legend on the plots at all, but it is a useful reference and I would like to keep it. 


Has anyone else had a similar problem or a solution?


The code in my function is something like this...


fig = plt.figure(figsize=(18, 5))

axes = plt.gca()

axes.set_ylim(minY, maxY)

plt.plot(distances, elevations, label='Terrain Profile', linestyle='dashed')

plt.legend(loc="upper right", numpoints = 1)