Select to view content in your preferred language

Arc Toolbox Python script tool plot legend error

1626
5
11-20-2017 02:30 PM
JamesSturman
Emerging Contributor

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')

Tags (1)
0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

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
JamesSturman
Emerging Contributor

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
DanPatterson_Retired
MVP Emeritus

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

JamesSturman
Emerging Contributor

Fantastic! Restarting ArcCatalog fixed it.

Great advice thanks again Dan.

0 Kudos
DanPatterson_Retired
MVP Emeritus

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