2

I encounter a segmentation fault: 11 when I use Matplotlib in an Django app.

I use :

python 2.7.8
matplotlib 1.4.2
OS X 10.9.5 (Mavericks)

I have seen that many people report this segfault 11:

On another OS: Segmentation fault in Django with matplotlib, without answer.

With another package: Segfault 11 with pandas with Python v2.7.6 RC1 on Mac OS X 10.9

With Python 3.3.2: Python segfault with OS X 10.9 Mavericks

The problem:

The segfault: 11 comes when I try to access the view.py in which matplotlib is used:

def cht(request):

    operations = Comptes.objects.all()
    ha = [0]
    he = [0]
    for i in operations:
        if i.commun==True and i.qui=='hadrien':
            ha.append(i.montant)
        if i.commun==True and i.qui=='helene':
            he.append(i.montant)

    x = range(0, 2)
    y = (sum(ha), sum(he))

    # the width of the bars
    width = 0.20
    # Call the figure and set its size
    f = plt.figure(figsize=(300,300))
    # Choose the size of the graph in the figure
    ax = f.add_axes([0.1, 0.1, 0.8, 0.8])
    # Plot the variables
    ax.bar(x, y, width, align='center', facecolor='green')
    plt.xlabel('Commun')
    plt.ylabel('Montant')
    plt.xticks(x)
    ax.set_xticklabels(['Aye', 'Bee'])
    plt.grid(True)

    canvas = FigureCanvasAgg(f)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)
    plt.close(f)

    return response

Here is the error message in the terminal :

Django version 1.7.1, using settings 'bud.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Segmentation fault: 11

What I've tried :

As recommended I upgraded my version of python and I have now python 2.7.8 but the problem remains.

I also unsuccessfully tried to apply a patch to the bug as recommended by the second answer of this thread : Segmentation fault: 11 in OS X.

EDIT:

I'm having the same problem outside of django with this code:

import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg

x = range(0, 2)
y = (50, 20)

width = 0.20
f = plt.figure(figsize=(300,300))

canvas = FigureCanvasAgg(f)
canvas.draw()

It gives me the same Segmentation fault: 11.

12
  • I'm currently facing the same problem. Did you manage to solve it somehow?
    – Jacobian
    Nov 16, 2014 at 15:25
  • No, I consider other thing than using Matplotlib (for example, flot in javascript...)
    – hadrienj
    Nov 16, 2014 at 16:15
  • Well, matplotlib is so powerful. And if you do for example some scientific work, then IMHO you would hardly find an alternative in javascript world.
    – Jacobian
    Nov 16, 2014 at 16:18
  • Sure! I totally agree. I will try to install Yosemite soon, I hope this will settle this.
    – hadrienj
    Nov 16, 2014 at 16:23
  • Can you reproduce the segfault outside of django?
    – tacaswell
    Nov 16, 2014 at 20:12

2 Answers 2

2

I guess, I have found a solution. At least now in my own app I do not have segfault anymore. So, the solution is to start Django server in this way:

python manage.py runserver --nothreading

Hope, it will help you.

1
  • and using nothreading defeats part of the point of using django as a sever.
    – tacaswell
    Nov 16, 2014 at 23:05
1

I was encountering similar 'segmentation fault 11' errors but for me it was using mercurial(hg)

This was trying to use Python 2.7.8 installed via the installer and pip install mercurial On OS X 10.9.5

At first I thought it was related to a readline.so error and tried renaming that (to avoid loading) although I found it hard to understand how that would help if it was being called upon. Then found the following thread regarding this.

Segmentation fault: 11 in OS X

In the end my solution seems to have come from installing python with homebrew and then installing python using that to get the 2.7.8 release (as of Dec 2014)

I then reinstalled mercurial with brew install mercurial which seems to have resolved whatever dependencies where causing this. I wish I understood better what was happening with the Seg fault but couldn't get to the bottom of it.

So, what I'm suggesting is updating the python install with brew and then reinstalling whatever other packages you depend on.

1
  • Thank you but actually the answer was in the comments... I will make it as an answer!
    – hadrienj
    Dec 4, 2014 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.