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\pyplot.py", line 3552, in legend
ret = gca().legend(*args, **kwargs)
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\axes\_axes.py", line 550, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\legend.py", line 385, in __init__
self._init_legend_box(handles, labels, markerfirst)
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\legend.py", line 654, in _init_legend_box
fontsize, handlebox))
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\legend_handler.py", line 119, in legend_artist
fontsize, handlebox.get_transform())
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\legend_handler.py", line 193, in create_artists
legline = Line2D(xdata, ydata)
File "C:\Python27\ArcGIS10.5\lib\site-packages\matplotlib\lines.py", line 286, in __init__
Artist.__init__(self)
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)

fig.savefig(OutFilePath)

plt.close('all')

Outcomes