Arc Toolbox Python script tool plot legend error

11-20-2017 02:30 PM
New Contributor II

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)



Tags (1)
0 Kudos
5 Replies
MVP Legendary Contributor

That error is internal to matplotlib and seems to have occurred when packaging.  That is going to be difficult to overcome since it may arise from a 'used name' conflict.  

As a check, you are importing matplotlib within your script aren't you?

0 Kudos
New Contributor II

Thanks Dan.

Yes I'm importing matplotlib right at the start of the script outside the functions like so...

import arcpy, os

from arcpy import env

import matplotlib.pyplot as plt

import numpy as np

The issue is frustrating and this morning when I first added the tool it appeared to work fine. I then had to make a few changes and it died. So it's not always consistent.

0 Kudos
MVP Legendary Contributor

nailing those down is hard... restart arcmap in between failures and ... what did you change! perhaps that is an issue that can be addressed

BTW  import arcpy last so that numpy and matplotlib (in theory) stake claim to namespace that may be duplicated further on during the arcpy import

New Contributor II

Fantastic! Restarting ArcCatalog fixed it.

Great advice thanks again Dan.

0 Kudos
MVP Legendary Contributor

Ahhh.. tip as well.  I find there are sometimes 'issues' when you run user tools from catalog, rather than running them from an added toolbox in arctoolbox.  Can't nail down what it is exactly... I have been told it doesn't make a difference, but I have seen it happen.. 

In short a reboot, makes you feel good when it works, so that is good


You might want to close the thread to show an answer was found

0 Kudos