I'm using sublime text 3 with REPL in python. My version of Python is python 3.5#64bits. It's not the Anaconda distribution, but a standalone version. I have been using pandas and numpy for a while without any troubles.
I am having trouble plotting with Matplotlib : my interpreter stop running after having call the show() method on a figure.
Using this code snippet, from the matplotlib documentation :
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
I get the following result. The second print("test") is never evaluated, and i have to restart REPL even if i close the plot's window.
My REPL settings are the following :
{
"cmd": ["C:\\Documents \\Sublime \\python-3.5.3.amd64\\python.exe", "-i", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell": true
}
I have so far tried to :
Switch matplotlib backends
According to matplotib documentation : I have switched to the interactive mode with ion() method / with the matplot.interactive(True)
Reinstall matplotlib
Use matplotilb wit bloc=False param
Does anyone know how to get the interpreter working after having plot ? Thanks in advance.
Related
I am trying to use Jupyter for Julia in vscode:
using PyPlot
a = [1,2]
b = [2,3]
fig=figure(1)
PyPlot.plot(a, b, linewidth=1)
PyPlot.scatter(a, b)
show()
When I run this a new window will pop up. What I want to do is to prevent it to pop up and see the picture below my codes. After doing some search it seems in Python you can solve it by %matplotlib inline, but how do I sovle it in Julia?
Try gcf() instead of show.
gcf yields the current PyPlot Figure object and it will be automatically rendered by Jupyter:
I am using Julia in Atom on a MacBook Pro and I do not succeed in getting a plot Window within or outside Atom when I use PyPlot.
Here is the package status :
....
(v1.1) pkg> status
Status `~/.julia/environments/v1.1/Project.toml`
[c52e3926] Atom v0.8.2
[7073ff75] IJulia v1.18.0
[e5e0dc1b] Juno v0.7.0
[d330b81b] PyPlot v2.8.0
[ade2ca70] Dates
...
I try the following code :
...
using PyPlot
plot(rand(10))
...
And I get :
...
1-element Array{PyCall.PyObject,1}:
PyObject <matplotlib.lines.Line2D object at 0x12963c9e8>
....
If I try :
...
plt.show()
...
I get :
...
/Users/Didier/.julia/conda/3/lib/python3.7/sitepackages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
% get_backend())
...
There are plenty discussions about Backends with PyPlot (i.e. Matplotlib inside Julia) but nowhere I found a rationale to modify the backend used by Matplotlib within Atom.
Does somebody have a clear idea about this?
Thanks in advance.
Use PyPlot.display_figs() as in this example:
using PyPlot
plot(1:5,(1:5).^2)
PyPlot.display_figs()
When working in the console PyPlot.show() could be a good idea.
Finally, you can always just save your picture to a file with the savefig command e.g. savefig(raw"c:\temp\aa.png")
I'd like to learn how to configure the defaults for matplotlib using the inline backend in jupyter notebook. Specifically, I'd like to set default 'figure.figsize’ to [7.5, 5.0] instead of the default [6.0, 4.0]. I’m using jupyter notebook 1.1 on a Mac with matplotlib 1.4.3.
In the notebook, using the macosx backend, my matplotlibrc file is shown to be in the standard location, and figsize is set as specified in matplotlibrc:
In [1]: %matplotlib
Using matplotlib backend: MacOSX
In [2]: mpl.matplotlib_fname()
Out[2]: u'/Users/scott/.matplotlib/matplotlibrc'
In [3]: matplotlib.rcParams['figure.figsize']
Out[3]:[7.5, 5.0]
However, when I use the inline backend, figsize is set differently:
In [1]: %matplotlib inline
In [2]: mpl.matplotlib_fname()
Out[2]: u'/Users/scott/.matplotlib/matplotlibrc'
In [3]: matplotlib.rcParams['figure.figsize']
Out[3]:[6.0, 4.0]
In my notebook config file, ~/.jupyter/jupyter_notebook_config.py, I also added the line
c.InlineBackend.rc = {'figure.figsize': (7.5, 5.0) }
but this had no effect either. For now I’m stuck adding this line in every notebook:
matplotlib.rcParams['figure.figsize']=[7.5, 5.0]
Is there any way to set the default for the inline backend?
The Jupyter/IPython split is confusing. Jupyter is the front end to kernels, of which IPython is the defacto Python kernel. You are trying to change something related to matplotlib and this only makes sense within the scope of the IPython kernel. Making a change to matplotlib in ~/.jupyter/jupyter_notebook_config.py would apply to all kernels which may not make sense (in the case of running a Ruby/R/Bash/etc. kernel which doesn't use matplotlib). Therefore, your c.InlineBackend.rc setting needs to go in the settings for the IPython kernel.
Edit the file ~/.ipython/profile_default/ipython_kernel_config.py and add to the bottom: c.InlineBackend.rc = { }.
Since c.InlineBackend.rc specifies matplotlib config overrides, the blank dict tells the IPython kernel not to override any of your .matplotlibrc settings.
If the file doesn't exist, run ipython profile create to create it.
Using Jupyter on windows at least, I was able to do it using something very much like venkat's answer, i.e.:
%matplotlib inline
import matplotlib
matplotlib.rcParams['figure.figsize'] = (8, 8)
I did this to square the circle, which had been rather eliptical up to that point. See, squaring the circle is not that hard. :)
Note that the path of ipython_kernel_config.py differs if you run ipython from a virtual environment. In that case, dig in the path where the environment is stored.
Use figsize(width,height) in the top cell and it changes width of following plots
For jupyter 5.x and above with IPython kernels, you can just override particular keys and leave the rest by putting things like this, with your desired figsize in your ~/.ipython/profile_default/ipython_kernel_config.py:
c = get_config()
c.InlineBackend.rc.update({"figure.figsize": (12, 10)})
I would like to use inline plotting in the ipython notebook, i.e.
%matplotlib inline
import matplotlib.pyplot as plt
x = np.arange(100)
plot(x, x**2)
should show an image.
Yet I only get the following message:
lib/python2.7/site-packages/IPython/core/formatters.py:239: FormatterWarning: Exception in image/png formatter: Could not create write struct
FormatterWarning,
What could be the reason for this?
matplotlib==1.3.1 and ipython==2.1.0
Does it say anything on the terminal (i.e. the server)?
My guess is that this is most probably due to some libpng incompatibility issues. If you are running this on OS X, the following discussion may help:
libpng version incompatibility in fresh installation of IPython
Even if you aren't running OS X, similar situations may occur, if you have several copies of libpng floating around.
I have the following code in IPython running IPython QT Console on Linux.
%pylab inline
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.
fig = figure()
ax = fig.add_axes()
ax = fig.add_axes([0,500, 0, 5000])
ax.plot([1,2,3,44], [4,4,55,55])
Out[5]: [<matplotlib.lines.Line2D at 0x3d8e7d0>]
fig
Out[6]: <matplotlib.figure.Figure at 0x3d25fd0>
fig.show()
/usr/lib/pymodules/python2.7/matplotlib/figure.py:362: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
"matplotlib is currently using a non-GUI backend, "
I've been struggling to make this work for some time, I've tried changing the backend manually with matplotlib.use() to Qt4Agg, GTK etc with no luck. This also happens in IPython notebook even when I call display().
Any ideas how to get the inline plotting working?
Marked Jakob's answer as the answer, but both are true actually. I had to replace the matploblibrc file with a new copy, started IPython QT Console with --pylab=None then manually entered %pylab inline in the console. Somehow this fixed the problem.
The axis object is defined incorrectly, this prevents matplotlib from rendering.
Remove the first ax = fig.add_axes(), and replace the second line with
ax = fig.add_axes([0, 0, 1, 1]).
The add_axes method requests the size of the axis in relative coordinates, in the form left, bottom, width, height with values between 0 and 1, see e.g. matplotlib tutorial.
You may also try fig.add_subplot(111) instead of fig.add_axes() or fig,ax = subplots() to create your figure and axis objects. The latter assumes that you have populated the interactive namespace matplotlib (%pylab) call in IPython.
It looks like your matplotlib build was compiled without a gui backend.
This is done when either a) it's explicitly specified (handy for webservers), or b) the required libraries for at least one gui backend aren't present (e.g. no Tk, Gtk, Qt, etc).
How did you install matplotlib?
If you compiled it from source, make sure that you have the development libraries for at least Tk installed and that your python install was compiled with Tk support (it is by default). If you installed it from your distro's repositories, whoever built the package built it without gui support, and you'll need to install it from another source.