matplotlib.pyplot.show() crashes ArcGIS

6742
15
Jump to solution
01-04-2016 02:13 AM
CanZhang
New Contributor

Hello everyone!


I'm creating a customizing tools in arcgis. I want to show some diagrams in arcgis, which made by matplotlib.

Everything works fine, the diagram kann even be saved in the right way, except when I show the plot through plt.show(), ArcGIS hangs and I have to force it to close with Task Manager.

Changing plt.show() to plt.show(block=False) should make the plot non-blocking, but this crashes ArcMap either, with a Runtime Error.

I am working with Windows 7 and ArcGIS 10.1.

The codes are something like:

import ConversionUtils

import arcpy

from arcpy import env

import matplotlib.pyplot as plt

xLabelName = ConversionUtils.gp.GetParameterAsText(0)

yLabelName = ConversionUtils.gp.GetParameterAsText(1)

Titel = ConversionUtils.gp.GetParameterAsText(2)

fig = plt.figure()
plt
.plot(range(10), range(10))

plt.xlabel(xLabelName, fontsize = 14)

plt.ylabel(yLabelName, fontsize = 14)

plt.title(str(Titel), fontsize = 16)

plt.grid()

plt.savefig('abc.png')

plt.show()

Because matplotlib uses TKinter by default, so the window pop up from plt.show() crashes ArcGIS.

The problem has been bothering me for days. I will greatly appreciate any ideas on how can I solve this Problem.

Thank you.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor

As an alternative, why not just let the default image viewer open the plot?

import os
import matplotlib.pyplot as plt

def plotthegraph():

    xLabelName = "x label" 
    yLabelName = "y_label" 
    title = "Title" 
    fig = plt.figure() 
    plt.plot(range(10), range(10)) 
    plt.xlabel(xLabelName, fontsize = 14) 
    plt.ylabel(yLabelName, fontsize = 14) 
    plt.title(str(title), fontsize = 16) 
    plt.grid() 
    plt.savefig(r'H:\abc.png')
    plt.close()
    os.system(r'H:\abc.png') #this should allow the installed image viewer to open the png

View solution in original post

0 Kudos
15 Replies
DanPatterson_Retired
MVP Emeritus

It will probably remain so, since no one can replicate the problem with only partial code.

0 Kudos
CanZhang
New Contributor

I have complemented the code.

0 Kudos
DanPatterson_Retired
MVP Emeritus

So the trick is to track down the source of the error. I edited the code to simplify it.

import ConversionUtils
import arcpy 
from arcpy import env 
import matplotlib.pyplot as plt
xLabelName = "x label"
yLabelName = "y_label"
title = "Title"
fig = plt.figure()
plt.plot(range(10), range(10))
plt.xlabel(xLabelName, fontsize = 14) 
plt.ylabel(yLabelName, fontsize = 14) 
plt.title(str(title), fontsize = 16)
plt.grid() 
plt.savefig("f:/test/abc.png") 
plt.show()

Basically I got rid of all that conversion stuff and tried to get output as a graph (see image) and as a file (see file).

It works...ergo...your problem is with your inputs using that ConversionUtils stuff.  At least your have a start point.

mpl_demo_line.png

0 Kudos
CanZhang
New Contributor

Yes, as you've tried, it works also fine only with python IDLE.

Because I use the code as the source of a customizing tool, so I need using that ConversionUtils). The problem is : the matplotlib utilizes Tkinter in it's backend.  I've had problems running Tkinter windows from within ArcMap.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Yes, Tkinter will cause crashes,  just google " tkinter arcmap crash " for some examples.

There are also posts for alternatives, for example python addins

python addin - Creating Tkinter dialog box in arcpy? - Geographic Information Systems Stack Exchange

But practically, you would be better off, using a conventional or python toolbox to get your input parameters rather than reinventing the wheel in a different dialog, if you intend to use this in arcmap

CanZhang
New Contributor

My input parameters are using a conventional GUI through ConversionUtils.gp.GetParameterAsText.

The Tkinter window is a output window, which pop up through plt.show().

After I run the tool/script, it shows an extra window with the plot diagram. Just like the output in IDLE.mpl_demo_line.png

Then I want to use this output window. After I usethe Pan/Zoom button, then click the save button in this window, it crashes arcgis. The diagram cannot be saved with this button, but I want to...

0 Kudos
DanPatterson_Retired
MVP Emeritus

Sorry...the window I showed works for me, don't know why yours doesn't.  Using Windows 10, ArcMap 10.3.1, tested in python 3.4.x and 2.7.x

0 Kudos
CanZhang
New Contributor

It doesn't work by me..I have to force it to close arcgis with Task Manager.

but thank you all the way.

0 Kudos
DanPatterson_Retired
MVP Emeritus

you might want to try it without the ConversionUtils module and either develop your app so that arcmap doesn't need to be open

0 Kudos