FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

segmentation fault when plotting large number of images · Issue #5795 · matplotlib/matplotlib · GitHub

Repository navigation

segmentation fault when plotting large number of images #5795

Closed

Description

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.

I originaly asked this as github question:
http://stackoverflow.com/questions/34592311/matplotlib-segmentation-fault-when-plotting-large-number-of-images

Activity

  1. QuLogic commented on Jan 6, 2016

    Member

    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.

  2. ProkopHapala commented on Jan 6, 2016

    Author

    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 )

  3. jenshnielsen commented on Jan 6, 2016

    Member

    I can't reproduce it either I ran the complete series in a virtual env on OSX 10.11.2 with:

    (testcrash) ✔ /tmp pip list
    funcsigs (0.4)
    matplotlib (1.4.3)
    mock (1.3.0)
    nose (1.3.7)
    numpy (1.9.2)
    pbr (1.8.1)
    pip (7.1.2)
    pyparsing (2.0.7)
    python-dateutil (2.4.2)
    pytz (2015.7)
    setuptools (19.2)
    six (1.10.0)
    wheel (0.26.0)
    (testcrash) ✔ /tmp python
    Python 2.7.11 (default, Dec  5 2015, 14:44:53)
    [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    

    And saw no segfault

  4. mdboom commented on Jan 6, 2016

    Member

    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.

  5. ProkopHapala commented on Jan 6, 2016

    Author

    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

  6. anntzer commented on Sep 18, 2017

    Contributor

    Closing due to lack of activity, but feel free to request a reopen if the issue is still present.

  7. 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:

    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

  8. gailweiss commented on Jul 1, 2019

    Update: problem persists even after upgrade to matplotlib 3.1.0

  9. reopened this on Jul 1, 2019
  10. tacaswell commented on Jul 2, 2019

    Member

    @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.

  11. tacaswell commented on Jul 2, 2019

    Member

    Can you also enable python's fault handler https://docs.python.org/3/library/faulthandler.html

    import faulthandler
    faulthandler.enable()

    at the top of your module.

  12. 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:

    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
    
    
  13. 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.

  14. oscargus commented on Jun 4, 2022

    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL