Pylab documentation - python

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())?

Related

Jupyter and %matplotlib inline lost axis

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.

Changing Figure Size in Sympy.mpmath.plot

I hope this question isn't too elementary. I've searched extensively for a solution but haven't discovered one yet.
I've recently begun using Jupyter Notebook with Sympy to take notes and do my homework in my Calculus II class (and what a HUGE BENEFIT this has been!).
However, my sole problem with it is that I'm unable to figure out how to configure the size (i.e. the dimensions in pixels) of the plot figure.
It's easy enough to do using matplotlib directly (matplotlib.pyplot.figure() specifically), but I'm using the Sympy.mpmath.plotmodule because Sympy works much better for the symbolic manipulation we're doing in this course. I know Sympy has its own plotting module, but the one in mpmath seems easier to use so far (with the exception of this one issue, of course).
However, I've looked through the mpmath documentation and have googled the problem repeatedly, without a solution.
How can you change the size of the image that results from plotting a function using the mpmath API?
You may try changing the size of sympy's plots via pyplot's rcParams:
import sympy
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = 10, 3
sympy.mpmath.plot([cos, sin], [-4, 4])

ipython --pylab vs ipython

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.

Figure-specific vs general properties in matplotlib

I am trying to understand how methods and attributes are organized in matplotlib. For example, say I have a figure:
import matplotlib.pyplot as plt
my_fig = plt.imshow(image)
I have noticed that some figure properties are set via module methods, e.g.:
plt.axis('off')
while others are set for the figure itself using object methods:
my_fig.set_cmap('hot')
Can figure properties be specified in either way?
How can I turn off the axis by calling methods on my object my_fig?
The plt methods are part of the pyplot API, which is intended to provide Matlab-like convenience for interactive use (and certainly appears to be very influenced by Matlab). But it's just one small facet of the whole matplotlib API (which is much more OOP). In practice I seem to end up mixing them both myself in SW; it's largely a matter of taste whether you go through the pyplot API or access the objects. pyplot is certainly very convenient although as you want to do more complex/exotic things you'll find what you can do with pyplot alone limited and you'll need to get to know at least the full API's Axes, Figure, Legend and Path objects better.
Pyplot is a collection of command style functions that make matplotlib work like MATLAB, matplotlib.figure.Figure is part of the object-oriented API.
In most cases you can configure figure settings via itself like this:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image=mpimg.imread('stinkbug.png')
my_fig = plt.imshow(image)
my_fig.axes.axes.get_xaxis().set_visible(False)
my_fig.axes.axes.get_yaxis().set_visible(False)
plt.show()
enter code here
required stinkbug.png:
result:

What is the difference between pylab and pyplot? [duplicate]

This question already has answers here:
Which is the recommended way to plot: matplotlib or pylab?
(2 answers)
Closed 1 year ago.
What is the difference between
matplotlib.pyplot and matplotlib.pylab?
Which is preferred for what usage?
I am a little confused, because it seems like independent from which I import, I can do the same things. What am I missing?
This wording is no longer in the documentation.
Use of the pylab import is now discouraged and the OO interface is recommended for most non-interactive usage.
From the documentation, the emphasis is mine:
Matplotlib is the whole package; pylab is a module in matplotlib that gets installed alongside matplotlib; and matplotlib.pyplot is a module in matplotlib.
Pyplot provides the state-machine interface to the underlying plotting library in matplotlib. This means that figures and axes are implicitly and automatically created to achieve the desired plot. For example, calling plot from pyplot will automatically create the necessary figure and axes to achieve the desired plot. Setting a title will then automatically set that title to the current axes object:
Pylab combines the pyplot functionality (for plotting) with the numpy functionality (for mathematics and for working with arrays) in a single namespace, making that namespace (or environment) even more MATLAB-like. For example, one can call the sin and cos functions just like you could in MATLAB, as well as having all the features of pyplot.
The pyplot interface is generally preferred for non-interactive plotting (i.e., scripting). The pylab interface is convenient for interactive calculations and plotting, as it minimizes typing. Note that this is what you get if you use the ipython shell with the -pylab option, which imports everything from pylab and makes plotting fully interactive.

Categories

Resources