I can plot in Python using either:
import matplotlib
matplotlib.pyplot.plot(...)
Or:
import pylab
pylab.plot(...)
Both of these use matplotlib.
Which is recommend as the correct method to plot? Why?
Official docs: Matplotlib, pyplot and pylab: how are they related?
Both of those imports boil down do doing exactly the same thing and will run the exact same code, it is just different ways of importing the modules.
Also note that matplotlib has two interface layers, a state-machine layer managed by pyplot and the OO interface pyplot is built on top of, see How can I attach a pyplot function to a figure instance?
pylab is a clean way to bulk import a whole slew of helpful functions (the pyplot state machine function, most of numpy) into a single name space. The main reason this exists (to my understanding) is to work with ipython to make a very nice interactive shell which more-or-less replicates MATLAB (to make the transition easier and because it is good for playing around). See pylab.py and matplotlib/pylab.py
At some level, this is purely a matter of taste and depends a bit on what you are doing.
If you are not embedding in a gui (either using a non-interactive backend for bulk scripts or using one of the provided interactive backends) the typical thing to do is
import matplotlib.pyplot as plt
import numpy as np
plt.plot(....)
which doesn't pollute the name space. I prefer this so I can keep track of where stuff came from.
If you use
ipython --pylab
this is equivalent to running
from pylab import *
It is now recommended that for new versions of ipython you use
ipython --matplotlib
which will set up all the proper background details to make the interactive backends to work nicely, but will not bulk import anything. You will need to explicitly import the modules want.
import numpy as np
import matplotlib.pyplot as plt
is a good start.
If you are embedding matplotlib in a gui you don't want to import pyplot as that will start extra gui main loops, and exactly what you should import depends on exactly what you are doing.
From the official documentation, as shown below, the recommendation is to use matplotlib.pyplot. This is not an opinion.
The documentation at Matplotlib, pyplot and pylab: how are they related?, which also describes the difference between pyplot and pylab, states: "Although many examples use pylab, it is no longer recommended.".
2021-05-06 Edit:
From The pylab API (disapproved)
Since heavily importing into the global namespace may result in unexpected behavior, the use of pylab is strongly discouraged. Use matplotlib.pyplot instead.
Related
It's not clear to me why plotting is done like this:
import pandas as pd
import matplotlib.pyplot as plt
df.boxplot(column='initial_cost', by='Borough', rot=90)
plt.show()
How is the dataframe tied to plt.show()? I've done a few web searches and even took a look at the documentation(!) but couldn't find anything addressing this specifically.
I would expect something more like:
boxplot = df.boxplot(column='initial_cost', by='Borough', rot=90)
plt.show(boxplot)
Or even something like this:
boxplot = df.boxplot(column='initial_cost', by='Borough', rot=90)
boxplot.plt.show()
Matplotlib provides a MATLAB-like state-machine, the pyplot module, that takes care under the hood of instantiating and managing all the objects you need to draw a plot.
Pandas hooks into that in the same fashion. When you call it takes care of loading pyplot and creating a matplotlib Figure, Axes, several Line2D objects and everything that makes a boxplot.
When you call plt.show() it will track all the figures you created with the state-machine API, create a GUI with those figures and take care of displaying it.
If you need more control, you can of course do it all yourself with the object-oriented API. Create a figure, axes, manually draw the canvas, it's all there if needed.
As far as I've seen the common practice is a mix of both: hook into the object-oriented API when needed but still let pyplot take care of displaying or saving everything to a file.
I am very curious to know how seaborn changes the behavior of matplotlib functions by just import seaborn as sns.
I want to realize the same function to change the behavior of imshow() function in pyplot, for example, i want to show the pixel value at the figure's low left corner.
It is of course possible to just redefine the imshow() function, and import the redefined ones, but the thing is I have multiple scripts that calls imshow() in many different ways, e.g., plt.imshow(), imshow(), and the OOP style axes.imshow(). Is there a simple way to do this like seaborn does?
Read seaborn's source code would of course give me some clue if I have the time luxury ...
Seaborn does not change the behavior of matplotlib functions in the way you describe. Matplotlib exposes a number of options for customization that take effect by changing the default values of various plot parameters. When seaborn is imported, it runs some code that uses this functionality to change the global defaults.
There is an important distinction between changing default parameter values and altering the behavior of functions. What you are proposing is the latter, and it is sometimes called monkey patching. It is possible, but it would be different than what seaborn is doing, and it isn't something I would recommend in any kind of production environment.
you can override whatever you want in python
my_pyplot.py
import matplotlib as mpl
def myPyPlot(*args,**kwargs):
print "You Said:",args,kwargs
mpl.pyplot = myPyPlot
main.py
import my_pyplot as mpp
from matplotlib import pyplot
print pyplot("arg1","arg2",axes="yellow")
note that you need to import your stuff before you import the modified stuff
When using matplotlib, I tend to use
import matplotlib.pyplot as plt
plt.style.use('ggplot')
quiet often. Is there an easy way to change the default style to ggplot (or any other style)? I looked into the 'matplotlibrc'-documentation but was unable to find an appropriate option.
Is there a better way then copying/linking the system-wide .mplstyle?
Thanks!
You can change the settings file of matplotlib. According to the docs :
matplotlib uses matplotlibrc configuration files to customize all kinds of properties, which we call rc settings or rc parameters. You can control the defaults of almost every property in matplotlib: figure size and dpi, line width, color and style, axes, axis and grid properties, text and font properties and so on.
You can locate your matplotlibrc file with the following command:
import matplotlib
matplotlib.matplotlib_fname()
Hence we can put ggplot settings at the end of the matplotlibrc file. You can easily find ggplot style (as well as other styles) on official matplotlib repository.
Apparently, there is no such option (yet).
However, you can tell iPython to load the ggplot style at startup, by adding "plt.style.use('ggplot')" to c.InteractiveShellApp.exec_lines in ipython_config.py.
Create a file (call it e.g. startup-01.py) in
~/.ipython/profile_default/startup/
(substitute another profile name for profile_default as needed)
and put there any needed notebook (and interactive ipython) initialization statements, including
import matplotlib.pyplot as plt
plt.style.use('ggplot')
## other settings, e.g.
# plt.rcParams['figure.figsize'] = (10.0, 8.0)
plt.style.use('default') worked for me.
As I understand this, it tells matplotlib to switch back to its default style mode.
When I look at the plotting style in the Pandas documentation, the plots look different from the default one. It seems to mimic the ggplot "look and feel".
Same thing with the seaborn's package.
How can I load that style? (even if I am not using a notebook?)
Update: If you have matplotlib >= 1.4, there is a new style module which has a ggplot style by default. To activate this, use:
from matplotlib import pyplot as plt
plt.style.use('ggplot')
To see all the available styles, you can check plt.style.available.
Similarly, for seaborn styling you can do:
plt.style.use('seaborn-white')
or, you can use seaborn's own machinery to set up the styling:
import seaborn as sns
sns.set()
The set() function has more options to select a specific style (see docs). Note that seaborn previously did the above automatically on import, but with the latest versions (>= 0.8) this is no longer the case.
If you actually want a ggplot-like syntax in Python as well (and not only the styling), take a look at the plotnine package, which is a grammar of graphics implementation in Python with a syntax very similar to R's ggplot2.
Note: the old answer mentioned to do pd.options.display.mpl_style = 'default'
. This was however deprecated in pandas in favor of matplotlib's styling using plt.style(..), and in the meantime this functionality is even removed from pandas.
For the themes in python-ggplot, you can use them with other plots:
from ggplot import theme_gray
theme = theme_gray()
with mpl.rc_context():
mpl.rcParams.update(theme.get_rcParams())
# plotting commands here
for ax in plt.gcf().axes:
theme.post_plot_callback(ax)
If you need to see available styles :
import matplotlib.pyplot as plt
print(plt.style.available)
This will print available styles.
And use this link to select the style you prefer
https://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html
Jan Katins's answer is good, but the python-ggplot project seems to have become inactive. The plotnine project is more developed and supports an analogous, but superficially different, solution:
from plotnine import theme_bw
import matplotlib as mpl
theme = theme_bw()
with mpl.rc_context():
mpl.rcParams.update(theme.rcParams)
While I think that joris answer is a better solution since you're using Pandas, it should be mentioned that Matplotlib can be set to mimic ggplot by issuing the command matplotlib.style.use('ggplot').
See examples in the Matplotlib gallery.
Can we control where Matplotlib places figures on the screen?
I want to generate four figures (in four separate windows) that do not overlap.
From IPython you can do the following:
figure()
get_current_fig_manager().window.wm_geometry("400x600+20+40")
Or equivalently in a Python script:
import pylab as pl
pl.figure()
pl.get_current_fig_manager().window.wm_geometry("400x600+20+40")
pl.show()
Note that this assumes you're using the TkAgg backend.
It is also possible to use the IPython interface with the Qt backend to achieve a similar result:
import matplotlib
import pylab as pl
f1 = pl.figure()
f_manager = pl.get_current_fig_manager()
f_manager.window.move(600, 600)
pl.show()
With f_manager you basically have a PyQt4 object that allows you to modify the window properties as you like.
Not using show() and Matplotlib alone. The simplest solution may be to use savefig(..) and use your favorite OS image viewer. If you need interactivity with the plots, Matplotlib offers backends.
The easiest way I know to do this is to make the window for the figure in your preferred GUI application, and then put the matplotlib figure into this window. There are a bunch of examples of how to do this embedding using different GUI frameworks here.
The code samples can look a bit complicated, but it's mostly boilerplate where you'll only need to modify a few lines.