What does ipython --pylab exactly do?
Is ipython --pylab exactly equivalent to:
> ipython
> from pylab import *
If not, what are the differences?
Say I launch IPython without the --pylab arguments, how can I bring it to the same state as if I had started it with --pylab?
--pylab[=option] is almost technically equivalent to %pylab option as the difference that you cannot un-pylab a --pylab kernel, but you can restart a %pylab kernel.
%pylab is a little more that just from pylab import * (see %pylab?for a longer explanation), but in short yes it imports a lot of things, but it also hooks event loops (qt, wx, osx...) and set-up some display hooks for matplotlib (the things that magically allow you to get inline graph). Setting the display-hook is closer to something like sympy.init_printing() if you wonder.
Note that starting at IPython 1.0 we recommend not to use --pylab or %pylab (unless you know exactly the implication). We provide %matplotlib that only init the display hook. %pylab will warn you if it replaced a few object in current namespace, and which ones. This is useful especially for functions like sum which do not have the same behavior the behavior with and without pylab and leads to subtle bugs.
We consider now that --pylab was a mistake, but that it was still really usefull at the beginning of IPython. We all know that Explicit is better than implicit so if you can advise people not to use %pylab we would appreciate it, to one day get rid of it.
Extract from %pylab help that give only the import part of pylab:
%pylab makes the following imports::
import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot
from IPython.display import display
from IPython.core.pylabtools import figsize, getfigs
from pylab import *
from numpy import *
One noticeable difference besides the imports is the interactive plotting, which you can enable dynamically with:
import matplotlib
matplotlib.rcParams['interactive'] = True
I think the --pylab option on the command line is equivalent to using the %pylab magic. At least that is how I have used it. That also gives you the opportunity to choose plotting backend, i.e. %pylab inline, %pylab qt, etc.
Related
When i plot in Spyder with the Backend Qt5 in graphics ( the window that pops with the plot) i don't get things aesthetically beautiful. The title sometimes is not showing, the labels, ... All in all it is just not good looking. But when it is plotted "inline" it is very well presented.
What I want is :
I want the plot with the Backend Qt5 option very well presented like the Backend Inline option.
The code is this :
import numpy as np
from matplotlib import pyplot as plt
plt.plot(np.linspace(0,2, num = 5))
plt.title('Blabla')
plt.xlabel('Anything')
plt.ylabel('Everything')
What I get with the Backend Qt5 option
What I get with the Backend Inline option
I think the command %matplotlib qt might be the solution to this problem. You should write the line in the IPython Console.
The problem was the use of this line in order to increase the quality of the plots:
plt.rcParams['figure.dpi'] = 300
In Qt5 it's not required.
I am having a really weird issue with using the %matplotlib inline code in my jupyter notebook for plotting graphs using both pyplot and the pandas plotting function.
The problem is they show up without any axes, and basically just show the graph area without anything aside from data points.
I found adding:
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
reverse it, but I find it odd that should do that every time as the effect disappears as soon as I run %matplotlib inlinecommand.
an example could be
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(A,A)
plt.tight_layout()
plt.xlabel('here')
plt.show()
This would generate the graph below:
Weird enough if I uses the savefig it get plotted with the axis, if I uses the right-click -> new output -> save as figure, I also get the graph with the figures !!
like this:
Can anyone help me understand what is wrong, which global setting did I mess up, and how do I revert it?
(I don't remember messing around with any settings aside from some settings for pandas, but don't think they should have had an impact)
as mentioned running mpl.rcParams.update(mpl.rcParamsDefault) command does bring it back to normal until I run %matplotlib inline` again !!
Any help would be much appreciated.
Okay I am sorry I think I can answer the question myself now.
With the helpfull #Mr. T asking for the imgur link made me realize what was going on. I had starting using the dark jupyter lab theme, and the graph would generate plots with transparent background, ie. the text and lines where there, but I just couldn't see them.
The trick is to change the background color preferably globally, but that will be a task for tomorrow.
This is not in a jupyter notebook so this is not a duplicate of this question, but my code is:
from gluoncv import model_zoo, data, utils
from matplotlib import pyplot as plt
...
plt.show()
The error I'm getting is:
/figure.py:445: UserWarning: Matplotlib is currently using ps, which is a non-GUI backend, so cannot show the figure.
% get_backend())
I created a repl at https://repl.it/#shamoons/WelloffHarmfulMineral
If it matters, I'm using OS X. What do I need to do to get the image to show?
You can use
matplotlib.use("TkAgg")
instead of
matplotlib.use("PS")
when developing on MacOS.
Please note that the import should be before importing plt, like this:
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
matplotlib.use('PS') and plt.show() are mutually exclusive. You need to decide:
Do you want to show the figure on screen? Solution: Remove the line matplotlib.use('PS').
Do you want to use the PS backend? This seems unlikely, because there is rarely a reason to set the backend to something non-interactive unless working on a server. Anyways, solution: Replace plt.show() by plt.savefig("filname.ps").
I visited the scipy site for PyLab. I could not find its documentation there. The matplotlib site also does not provide any information on it.
Where can I find a tutorial/documentation on PyLab?
Pylab is basically just Numpy and Matplotlib under a unified namespace. Learn about either of those and you will understand Pylab.
If you want to plot things in scripts it is generally preferred that you use import matplotlib.pyplot instead of import pylab, but really the choice is up to you.
If you want to have interactive plotting (for instance, by calling ipython --pylab) then pylab is the way to go. However pyplot can also be put in an interactive mode using pyplot.ion().
Some more information can be found here:
What is the difference between pylab and pyplot?
Exact semantics of Matplotlib's "interactive mode" (ion(), ioff())?
I am new to the world of mac. In python we I want to visualize a data, I use matplotlib's pyplot to generate a plot but when I do pyplot.show() it creates a new window. This behavior also happens inside my ipython notebook too - see image below. I wanted it to embed the image inside the notebook.
How can I correct this ?
Try ipython notebook --pylab inline when you launch ipython.
Input in a cell, the cell magic %pylab inline or %matplotlib inline will make the plots appear inline/interactive (instead of a new window).
Not that if you use %matplotlib cell magic, you still need import matplotlib.pyplot as plt and import numpy as np etc. With %pylab cell magic you don't.