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.

Community
  • 1
  • 1
hhh
  • 1,913
  • 3
  • 27
  • 35
  • I'm currently facing the same problem. Did you manage to solve it somehow? – Jacobian Nov 16 '14 at 15:25
  • No, I consider other thing than using Matplotlib (for example, flot in javascript...) – hhh Nov 16 '14 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 '14 at 16:18
  • Sure! I totally agree. I will try to install Yosemite soon, I hope this will settle this. – hhh Nov 16 '14 at 16:23
  • Can you reproduce the segfault outside of django? – tacaswell Nov 16 '14 at 20:12
  • Well, I finally narrowed down this problem to its main cause. It all happened because of `pyplot`. The `matplotlib` by itself works ok, and if you use simple plots - then everything is just smooth, but just one line of code `plt.figure` which you have and which I have in my app results in a terrible crash. – Jacobian Nov 16 '14 at 21:15
  • And by the way, I just ported my app to Windows and also see the same problem here – Jacobian Nov 16 '14 at 21:17
  • @tcaswell Outside of django: see the edit. Another thing I run the codes into a virtual environment (but exactly the same problem in the global env). – hhh Nov 16 '14 at 22:15
  • Why are you trying to make a 300inch x 300inch figure? That is a 300*300*72*72 = 466,560,000 pixel figure, try turning the size down to `(3, 3)`. – tacaswell Nov 16 '14 at 23:12
  • @tcaswell Whoa! Well done, it works! – hhh Nov 17 '14 at 09:02
  • @tcaswell Could you make it as an answer? – hhh Nov 18 '14 at 09:21
  • @tcaswell Ok I will answer my own question with this to avoid other people take time to answer. Thanks again! – hhh Dec 04 '14 at 09:45

2 Answers2

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.

Jacobian
  • 10,122
  • 29
  • 128
  • 221
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.

Community
  • 1
  • 1
Brian Lamb
  • 21
  • 3
  • Thank you but actually the answer was in the comments... I will make it as an answer! – hhh Dec 04 '14 at 09:43