matplotlib show() prevents script from completing

66523
6
10-28-2016 07:46 AM
TimothyHales
Esri Notable Contributor

I have a script tool that creates a graph using matplotlib. I use plt.show() to display the graph, but the script does not continue until I close the window. Thanks to Dan Patterson‌ and /blogs/dan_patterson/2014/08/19/x-y-graphing-demo-using-pyplot-matplotliban-introduction?sr=search&s...‌ I found plt.close(), but is there a way to allow the script to continue while keeping the plot window open?

import matplotlib.pyplot as plt

#Processing Step

#Create Graph
tName = "tableLocation.txt"
outputFile = "outputFigure.png"

x = []
y = [] 

with open(tName) as f:
    next(f)
    for line in f:
        parts = line.split(",")
        x.append(parts[2])
        y.append(parts[3])

fig = plt.figure()
plt.plot(x, y)
plt.xlabel('Distance')
plt.ylabel('Elevation')
fig.suptitle("Graph Title")
plt.show()
fig.savefig(outputFile)

#Next Processing Step
0 Kudos
6 Replies
TimothyHales
Esri Notable Contributor

Maybe I should have dug a little deeper before asking, but it looks like this is the solution:

plt.show(block=False)

 

But now that works, I get a RunTime Error and ArcMap crashes when I interact with the graph (Click window or close).

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Looking at the matplotlib.pyplot.show documention:

A single experimental keyword argument, block, may be set to True or False to override the blocking behavior described above.

ArcMap bug or experimental keyword not working?  I assume the experimental keyword doesn't crash all other apps all the time, so maybe open a bug report with Esri Support.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Try

plt.close()

0 Kudos
Luke_Pinner
MVP Regular Contributor

Perhaps run the plotting as a separate script via a subprocess. 

DanPatterson_Retired
MVP Emeritus

hmmmm some better options... to test but here is a link... I will copy a few for posterity since they are on github

http://central.scipy.org/item/84/1/simple-interactive-matplotlib-plots by Thomas Haslawanter 2015

TimothyHales
Esri Notable Contributor

Thanks for all of the suggestions. I decided not to show the chart through the matplotlib module. Since the features within that window were limited I saved it out as an image, and opened it in the default picture viewer. This allowed the process to complete without being held up by the chart window.

0 Kudos