Select to view content in your preferred language

matplotlib.pyplot.show() crashes ArcGIS

7688
15
Jump to solution
01-04-2016 02:13 AM
CanZhang
Deactivated User

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
15 Replies
JamesCrandall
MVP Frequent Contributor

I didn't see it in your code example (or in Dan' either), but you should close the plt --- include this line after the plt.show():

plt.close()

See if that stops the instability you are experiencing.

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

James, I think the problem he is having is that he has an extra instance of a Tkinter window opening through some means.  They don't behave with ArcMap, but I have had no problem with matplotlib figures opening ... I can even flip back and forth to the graph window and arcmap.  If you try to have other Tk dialogs open, there are "issues" which for some undocumented reasons, ArcMap doesn't behave nicely in the presence of Tkinter even though it is distributed with python.  There are work arounds, but they are akin to getting a car to move with square wheels.

JamesCrandall
MVP Frequent Contributor

I've seen weirdness at times too.  But if you do not instantiate plt.show(), simply save the fig, close it and then just allow os.system to open it using whatever default Windows application that typically views .png files to open it should remove the instability issues because a Tkinter window is not being used.

CanZhang
Deactivated User

Hi James, right, this is maybe an alternativ solution, so that the TK window won't show up in arcgis.

ps: plt.close() doesn't work, because plt.show() will suspend the process/script, until man close the window manual.

0 Kudos
JamesCrandall
MVP Frequent Contributor

You definitely want to include plt.close() after you have finished with writing the plot to a file!!! Google it

0 Kudos