<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Script Tool Fails when Adding a Legend in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/script-tool-fails-when-adding-a-legend/m-p/16912#M1315</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you are using Pro, matplotlib will run.. as long as you don't get the script to close the graph which kindof can cause other problems.&lt;/P&gt;&lt;P&gt;There are others in the Anaconda suite installed with pro, depending on the type of graphing and the purpose of the graphing ie&amp;nbsp;&lt;A class="link-titled" href="https://orange.biolab.si/" title="https://orange.biolab.si/"&gt;Orange – Data Mining Fruitful &amp;amp; Fun&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 13 Jun 2018 20:42:24 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2018-06-13T20:42:24Z</dc:date>
    <item>
      <title>Script Tool Fails when Adding a Legend</title>
      <link>https://community.esri.com/t5/python-questions/script-tool-fails-when-adding-a-legend/m-p/16909#M1312</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a Python script that I'm using in a script tool in ArcToolbox. This script reads FAO food security indicator data and then plots the data for a country that the user chooses. When I run the script outside of ArcGIS, it plots the data with no issues.&lt;/P&gt;&lt;P&gt;However, when the script tool is run within ArcGIS, I receive an error the second time it is run. The error is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;"An error occurred: expected string or buffer"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The first time the script tool runs, everything works fine: it outputs a figure with food security information. It's the second time I run the tool that it fails and returns the above error.&lt;/P&gt;&lt;P&gt;A similar question was asked here:&amp;nbsp;&lt;A href="https://community.esri.com/thread/205447"&gt;Arc Toolbox Python script tool plot legend error &lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And ultimately the solution that was arrived at was to restart ArcGIS. When I restart ArcGIS, I am able to successfully run the tool again, but, as expected, it fails on the subsequent run. This will be inconvenient for the users of this tool since I don't want them to have to restart ArcGIS every time they run the tool for a different country or different food security indicator.&lt;/P&gt;&lt;P&gt;I've identified in the script where the issue occurs. I've imported matplotlib.pyplot as plt, and when my script includes 'plt.legend' in order to add a legend to the figure, this is what causes the script to fail on the second run. If I remove plt.legend from the script, I can run the tool as many times in succession as I want without an error (unfortunately, the plots that are created no longer have a legend).&lt;/P&gt;&lt;P&gt;Has anyone encountered this issue in the past? If so, have you found a way to overcome this without restarting ArcGIS every time?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's my code where I add the legend:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;plt.figure(figsize = (12, 9))&lt;/P&gt;&lt;P&gt;ax = plt.subplot(111)&lt;/P&gt;&lt;P&gt;ax.get_xaxis().tick_bottom()&lt;/P&gt;&lt;P&gt;ax.get_yaxis().tick_left()&lt;/P&gt;&lt;P&gt;ax.tick_params(axis = "both", labelsize = 10)&lt;/P&gt;&lt;P&gt;lineDrawType = [(5, 2, 20, 2), (2, 5), (4, 4, 2, 2), (4, 10), (5, 2)]&lt;/P&gt;&lt;P&gt;obsList = [iso_code, "lowestIncome", "midLowerIncome", "midUpperIncome", "uppermostIncome"]&lt;/P&gt;&lt;P&gt;labelObsNames = [countryName, "Low-Income", "Lower-Middle-Income", "Upper-Middle-Income", "High-Income"]&lt;/P&gt;&lt;P&gt;for rank, locationName in enumerate(obsList):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;df_plot = df_selected[df_selected["ISO_code"] == locationName] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;plt.plot(df_plot["YearEnd"], df_plot["Value"], lw = 3., color = tableau[rank], ls = "--", dashes = lineDrawType[rank], &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;label = labelObsNames[rank])&lt;/P&gt;&lt;P&gt;artList = []&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;lgdPlotter = plt.legend(loc = "upper center", bbox_to_anchor = (0.5, -0.1), ncol=3)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;artList.append(lgdPlotter)&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;plt.savefig(os.path.join(outputLocation, "test2.png"), additional_artists = artList, bbox_inches = "tight")&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;plt.close("all")&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jun 2018 14:57:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-tool-fails-when-adding-a-legend/m-p/16909#M1312</guid>
      <dc:creator>PaulMcCord1</dc:creator>
      <dc:date>2018-06-13T14:57:57Z</dc:date>
    </item>
    <item>
      <title>Re: Script Tool Fails when Adding a Legend</title>
      <link>https://community.esri.com/t5/python-questions/script-tool-fails-when-adding-a-legend/m-p/16910#M1313</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;matplotlib doesn't play nice sometimes when arcmap is open.. it does behave somewhat better in PRO.&lt;/P&gt;&lt;P&gt;As in the other post, I don't use Catalog to run scripts or toolbox scripts. &amp;nbsp;&lt;/P&gt;&lt;P&gt;If I need to use matplotlib, I comment out plt.close() lines and manually close them when running a script from a toolbox... I don't know why but I suspect there is a small battle between the applications.&lt;/P&gt;&lt;P&gt;Running the scripts as a standalone is preferable and if you need an interface, qt is installed by default with PRO&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jun 2018 15:37:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-tool-fails-when-adding-a-legend/m-p/16910#M1313</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2018-06-13T15:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: Script Tool Fails when Adding a Legend</title>
      <link>https://community.esri.com/t5/python-questions/script-tool-fails-when-adding-a-legend/m-p/16911#M1314</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you Dan,&lt;/P&gt;&lt;P&gt;Unfortunately, I need to include this as a script tool belonging to a larger suite of script tools that are shipped in an Arc Toolbox, so the users won't have the option to run as a standalone script.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since there seems to be this conflict between the applications, I might see if another library is able to plot my data without the conflict.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you, Paul&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jun 2018 20:09:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-tool-fails-when-adding-a-legend/m-p/16911#M1314</guid>
      <dc:creator>PaulMcCord1</dc:creator>
      <dc:date>2018-06-13T20:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: Script Tool Fails when Adding a Legend</title>
      <link>https://community.esri.com/t5/python-questions/script-tool-fails-when-adding-a-legend/m-p/16912#M1315</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you are using Pro, matplotlib will run.. as long as you don't get the script to close the graph which kindof can cause other problems.&lt;/P&gt;&lt;P&gt;There are others in the Anaconda suite installed with pro, depending on the type of graphing and the purpose of the graphing ie&amp;nbsp;&lt;A class="link-titled" href="https://orange.biolab.si/" title="https://orange.biolab.si/"&gt;Orange – Data Mining Fruitful &amp;amp; Fun&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jun 2018 20:42:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-tool-fails-when-adding-a-legend/m-p/16912#M1315</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2018-06-13T20:42:24Z</dc:date>
    </item>
  </channel>
</rss>

