matplotlib CGI doesn't work - apache

I'd like to publish a figure generated by matplotlib on web site based on Windows Apache Server.
A Scipy example code (below) shows just tiny icon which link is broken.
The code of related question on this BBS also just shows icon.
My code is below. I guess the code is correct, but I probably mess up some setting of Apache Server or matplotlib. Any comments is welcome.
#!c:/python27/python
import os, sys
import cgi
import cgitb; cgitb.enable()
os.environ['HOME'] = r'C:\temp'
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.plot([1,2,1,4,2,6,4,8])
print "Content-Type: image/png\n"
plt.savefig( sys.stdout, format='png' )

Related

matplotlib doesn't show plot in docker

I was running into this error:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
However, even if I added:
matplotlib.use('TkAgg')
in my script, plt.show() would still not work.
The issue was, in one of my imported files, I was calling:
matplotlib.use('Agg')
Once a GUI backend is loaded, apparently it cannot be changed without issues.
After commenting out:
matplotlib.use('Agg')
In my import, everything works as intended.

import matplotlib.pyplot crashes on jupyter notebook

I have a jupyter notebook running on a remote cluster to which I have set up an ssh tunnel. Everything was working fine till today. Now everytime I do :
import matplotlib # This works
%matplotlib inline # This causes kernel to restart
import matplotlib.pyplot # This also causes the kernel to restart
Running a standalone ipython interpreter and doing :
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot ## Leads to Core dumped : Segementation Fault
Running the same on a python interpreter works fine.
Jupyter version : 4.1.1
Python version : 2.7.7
Any help would be much appreciated.
Thank You
Often, this kind of error seems to be related to the backend. Have you tried any other backends? Do these result in the same error? Like this we could narrow down the source of the error.
(I don't have a remote cluster, so I can not reproduce it.)
You can find available backends as described here.
I perhaps have the same problem but on my local machine. I got into jupyter3-qtconsole 4.2.1 with Python 3.4.5 and IPython 5.0.0. and enter
`%matplotlib
Using matplotlib backend: Qt4Agg`
the error message (shortened):
File "/usr/lib64/python3.4/site-packages/tornado/ioloop.py", line 603, in _run_callback
ret = callback()
and finally
from IPython.core.interactiveshell import NoOpContext as context
ImportError: cannot import name 'NoOpContext'
Same thing happens in a notebook but in a straightforward IPython terminal, everything runs OK
Hope this is helpful to someone

wx script can't see numpy, but it's installed

I had a wx script working on winxp (at work). it was upgraded to win7_64. I installed python2 and wxpython (both 32bit). now my script doesn't want to run. it says "ImportError: NumPy not found.". so I installed numpy from numpy.org, but it didnt change anything. I can import wx, I can import numpy, but when I try to run my wx script, it says that numpy is not installed. I removed and reinstalled everything but nothing changed.
what to do?
Presumably your numpy is too "new" or your wxPython is too old.
For example the combination wxPython < 3.0 and numpy > 1.9 will not work for the plot module (2.9.5 + numpy 1.8.0 and 3.0.2 + numpy 1.9.2 do actually work).
Reason should be file <site-packages.wx>/lib/plot.py (2.9.5):
# Needs NumPy
try:
import numpy.oldnumeric as _Numeric
except:
msg= """
This module requires the NumPy module, which could not be
imported. It probably is not installed (it's not part of the
standard Python distribution). See the Numeric Python site
(http://numpy.scipy.org) for information on downloading source or
binaries."""
raise ImportError, "NumPy not found.\n" + msg
and as used in 3.0.2):
# Needs NumPy
try:
import numpy as np
except:
numpy.oldnumeric is no longer part of numpy 1.9.2, wx.lib.plot was developed for ancient array libraries and you can clearly see its age.

Setting Matplotlib MPLCONFIGDIR: consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data

I am using Linux server to set up a django project. I got this error: "Failed to create /var/www/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"
Then I found $MPLCONFIGDIR are empty. So I set it like this:
lab#base:~$ export MPLCONFIGDIR=~/website/graph
lab#base:~$ echo $MPLCONFIGDIR
/home/lab/website/graph
This path is the directory where I want to store images created by Matplotlib.
Then I made sure this setting in python command line:
>>> import matplotlib
>>> import os
>>> os.environ.get('MPLCONFIGDIR')
'/home/lab/website/graph'
BUT, in the django project which is deployed in Apache with mod_wsgi, the above-mentioned error still exits. I added the below lines:
import os
os.environ['MPLCONFIGDIR'] = "/home/lab/website/graph"
print(os.environ.get('MPLCONFIGDIR'))
It prints "None"!
Can anyone help me?
Thanks.
Set the MPLCONFIGDIR in code before you import matplotlib. Make sure the directory has permissions such that it can be written to by the app.
import os
os.environ['MPLCONFIGDIR'] = "/home/lab/website/graph"
import matplotlib
Alternatively, you could set it to a tempfile.
import os
import tempfile
os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
import matplotlib
Per #Esteban I do something like this in modules or scripts:
import os
try:
import matplotlib
except:
import tempfile
import atexit
import shutil
mpldir = tempfile.mkdtemp()
atexit.register(shutil.rmtree, mpldir) # rm directory on succ exit
os.environ['MPLCONFIGDIR'] = mpldir
import matplotlib
This way the temporary directory is removed on exit.

Use glade with pygobject Gtk3

I am converting a script to use Gtk3 using the migration guide (Porting GTK2 to GTK3). I converted my import pygtk to a from gi.repository import Gtk and so on...
I'm stuck because the glade module was loaded from module gtk:
import gtk
import gtk.glade
but there's no way now to do that anymore.
Note that I would only need a replacement for gtk.glade.XML()...
Well, the solution is pretty obvious, after calling to Gtk.Builder() one needs to convert the old glade interface with the gtk-builder-convert command to get the interface file in the right version.
$ gtk-builder-convert myui.glade myui.ui
And then, in the python script:
from gi.repository import Gtk
builder = Gtk.Builder()
builder.add_from_file("myui.ui")
Thanks to Riccardo.
This should work
from gi.repository import Gtk
builder = Gtk.Builder()
builder.add_from_file("project.xml")