Hello, when I plot few hundred of images the matplotlib crash with segmentation fault. This is the minimal code which reproduce the error ( I generate random 3D datagrids and then plot each slice and store figure to file ):
import numpy as np
import matplotlib.pyplot as plt
default_figsize = (3,3)
def plotImages( prefix, F, figsize=default_figsize ):
slices = range( len( F ) )
for ii,i in enumerate(slices):
print " plotting ", i
plt.figure( figsize=figsize )
plt.imshow( F[i], origin='image' )
plt.title( r"iz = %i" %i )
plt.savefig( prefix+'_%3.3i.png' %i, bbox_inches='tight' )
plt.close()
nx = 500
ny = 500
nz = 100
ng = 100
for i in range( ng ):
F = np.random.randn( nz,ny,nx )
print "NEW GRID ", i , " : ", np.shape(F)
plotImages( "", F )
del F
in this case the error happened after at grid 12 image 54 ( i.e. 11*100 + 54 = 1154 images ). But the exact point of crash depends on many parameters (size of grids, size of images, and probably also memory usage on my machine ). I do not know how to trace it.
With what versions of, well, everything? I tried this script with Python 2.7, 3.4, and 3.5 with either 1.4.3 or nearly latest master and did not run into any issue. Memory usage was never more than 0.5G.
In my case it also does not take too much memory, just do the segfault, which position depend on memory usage ( how many aps I have open ... ) ( but it is just my feeling, I never run it more than 3 times with exactly the same settings )
How did you build matplotlib? Often segfaults are due to the C++ extensions not getting completely rebuilt between version updates. Doing a git clean -fxd before rebuilding can often help.
Hi, please reopen this issue. Able to create a segmentation fault using this code:
create file plotter.py as follows:
import matplotlib.pyplot as plt
def make_n_plots(num_plots):
for i in range(num_plots):
plt.scatter(range(3),[1+i,2+i,3+i])
plt.savefig("segplots_"+str(i)+".png")
plt.close()
print("everything seems fine, but it ain't over til the fat lady sings",flush=True)
return "lalalalala"
Now go to the console and try:
from plotter import *
make_n_plots(20)
this will return "lalalalala", but then crash and show: Segmentation Fault: 11 (occasionally similar configurations cause an Abort trap: 6, but I cannot exactly remember how to construct them).
What's interesting is that this only happens if you make several plots 'right off the bat'. For instance, it will not crash if you first call the function with less plots, and only then try the big one, i.e. the following doesn't cause a segmentation fault:
from plotter import *
make_n_plots(10)
make_n_plots(20)
Using:
Python 3.6.8 |Anaconda, Inc.| (default, Dec 29 2018, 19:04:46)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
matplotlib version: 2.1.0
macOS Mojave 10.14.5
Does it work reliably if you do matplotlib.use('agg') before importing plt? My knee-jerk guess is that this is related to creating, but not showing many OSX windows.
import matplotlib
print("plotter using matplotlib with backend:",matplotlib.get_backend())
to the top of plotter.py shows it is using TkAgg. (Here make_n_plots still ends with a segfault when called from the console.)
I think it's also important to note that it is the return to the terminal/python that seems to crash, i.e. only if the function is called inside the terminal after opening python: if I make a further python file call_plotter.py that does what I would otherwise do in the terminal (after calling python3), and run it directly with ./call_plotter.py, then there isn't a segmentation fault.
Regarding your suggestions:
adding
import matplotlib
matplotlib.use('agg')
to the file, before import matplotlib.pyplot as plt, worked! Thank you!
For debug purposes
if it interests you, I ran it without matplotlib.use('agg') but with the faulthandler as you suggested.
Here's the full print, but I don't think it's very informative:
mylap:matplotlib_segfault myuser$ python3
Python 3.6.8 |Anaconda, Inc.| (default, Dec 29 2018, 19:04:46)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from plotter import *
>>> make_n_plots(20)
everything seems fine, but it ain't over til the fat lady sings
'lalalalala'
>>> Fatal Python error: Segmentation fault
Current thread 0x000000010efc45c0 (most recent call first):
Segmentation fault: 11
I'm going to close this again. There has been some improvements and I cannot reproduce it on Windows using either TkAgg or QtAgg, Also, as pointed out in #20300 using a GUI-backend and not show the figure is not really the way to go (use a non-GUI backend, like agg).
Activity
QuLogic commented on Jan 6, 2016
With what versions of, well, everything? I tried this script with Python 2.7, 3.4, and 3.5 with either 1.4.3 or nearly latest master and did not run into any issue. Memory usage was never more than 0.5G.
ProkopHapala commented on Jan 6, 2016
Python 2.7.3
numpy.version = 1.9.2
matplotlib.version = 1.4.3
ubuntu 12.04 LTS 64bit
In my case it also does not take too much memory, just do the segfault, which position depend on memory usage ( how many aps I have open ... ) ( but it is just my feeling, I never run it more than 3 times with exactly the same settings )
jenshnielsen commented on Jan 6, 2016
I can't reproduce it either I ran the complete series in a virtual env on OSX 10.11.2 with:
And saw no segfault
mdboom commented on Jan 6, 2016
How did you build matplotlib? Often segfaults are due to the C++ extensions not getting completely rebuilt between version updates. Doing a git clean -fxd before rebuilding can often help.
ProkopHapala commented on Jan 6, 2016
hi ... I did not rebult it, I istalled it somehow from binary (don't remeber how; probably by ubuntu software manager ... )
ok ... I will try to reinstall it, or try it on other computer
anntzer commented on Sep 18, 2017
Closing due to lack of activity, but feel free to request a reopen if the issue is still present.
gailweiss commented on Jul 1, 2019
Hi, please reopen this issue. Able to create a segmentation fault using this code:
create file plotter.py as follows:
import matplotlib.pyplot as plt def make_n_plots(num_plots): for i in range(num_plots): plt.scatter(range(3),[1+i,2+i,3+i]) plt.savefig("segplots_"+str(i)+".png") plt.close() print("everything seems fine, but it ain't over til the fat lady sings",flush=True) return "lalalalala"Now go to the console and try:
this will return "lalalalala", but then crash and show: Segmentation Fault: 11 (occasionally similar configurations cause an Abort trap: 6, but I cannot exactly remember how to construct them).
What's interesting is that this only happens if you make several plots 'right off the bat'. For instance, it will not crash if you first call the function with less plots, and only then try the big one, i.e. the following doesn't cause a segmentation fault:
Using:
Python 3.6.8 |Anaconda, Inc.| (default, Dec 29 2018, 19:04:46)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
matplotlib version: 2.1.0
macOS Mojave 10.14.5
gailweiss commented on Jul 1, 2019
Update: problem persists even after upgrade to matplotlib 3.1.0
tacaswell commented on Jul 2, 2019
@gailweiss what backend are you using?
Does it work reliably if you do matplotlib.use('agg') before importing plt? My knee-jerk guess is that this is related to creating, but not showing many OSX windows.
I can not reproduce this with Qt5Agg on linux.
tacaswell commented on Jul 2, 2019
Can you also enable python's fault handler https://docs.python.org/3/library/faulthandler.html
at the top of your module.
gailweiss commented on Jul 2, 2019
Further details on previous description:
adding
import matplotlib print("plotter using matplotlib with backend:",matplotlib.get_backend())to the top of plotter.py shows it is using TkAgg. (Here make_n_plots still ends with a segfault when called from the console.)
I think it's also important to note that it is the return to the terminal/python that seems to crash, i.e. only if the function is called inside the terminal after opening python: if I make a further python file call_plotter.py that does what I would otherwise do in the terminal (after calling python3), and run it directly with ./call_plotter.py, then there isn't a segmentation fault.
Regarding your suggestions:
adding
import matplotlib matplotlib.use('agg')to the file, before import matplotlib.pyplot as plt, worked! Thank you!
For debug purposes
if it interests you, I ran it without matplotlib.use('agg') but with the faulthandler as you suggested.
Here's the full print, but I don't think it's very informative:
jengelman commented on Nov 10, 2019
I've been reliably reproducing this issue on Windows, Python 3.7.5, matplotlib 3.1.1, and likewise fixed by switching the backend to Agg.
oscargus commented on Jun 4, 2022
I'm going to close this again. There has been some improvements and I cannot reproduce it on Windows using either TkAgg or QtAgg, Also, as pointed out in #20300 using a GUI-backend and not show the figure is not really the way to go (use a non-GUI backend, like agg).
Feel free to reopen if it is still a problem.