Matplotlib figure size in Jupyter reset by inlining in Jupyter - python

This question is more of a curiosity.
To change the default fig size to a custom one in matplotlib, one does
from matplotlib import rcParams
from matplotlib import pyplot as plt
rcParams['figure.figsize'] = 15, 9
after that, figure appears with chosen size.
Now, I'm finding something new (never happened/noticed before just now): in a Jupyter notebook, when inlining matplotlib as
%matplotlib inline
this apparently overwrites the rcParams dictionary restoring the default value for the figure size. Hence in oder to be able to set the size, I have to inline matplotlib before changing the values of the rcParams dictionary.
I am on a Mac OS 10.11.6, matplotlib version 1.5.1, Python 2.7.10, Jupyter 4.1.

IPython's inline backend sets some rcParams when it is initialized. This is configurable, and you can override it with your own configuration:
# in ~/.ipython/ipython_config.py
c.InlineBackend.rc = {
'figure.figsize': (15, 9)
}
The above would replace all of the rcParams that the inline backend sets, and you get total control. If you already have a matplotlib style that works nicely for inline output, you can tell the backend to leave everything alone:
c.InlineBackend.rc = {}
If you want to change just a few values, rather than overriding the whole thing, you can use the dictionary .update method:
c.InlineBackend.rc.update({'figure.figsize': (15, 9)})
In the future, the inline backend should be doing its defaults via matplotlib's nice new style mechanism, which should make it behave nicer in terms of respecting your preferences and allowing easier customization.

Related

Jupyter notebook: How to set matplotlib figure default size persistently, even after `from matplotlib import pyplot`?

I'm looking for a way to configure my default notebook set up and set the size of plots and figures, so that I do not have to set it manually for each notebook individually.
I'm aware of questions like How to set the matplotlib figure default size in ipython notebook? which detail how to use jupyter_notebook_config.py.
However this isnt working, presumably because I reimport pyplot in the notebook. Is there a better way to set default settings, behaviors, options etc?
In python 3.x
ipython profile create
ipython profile locate
go to the locate and edit ipython_kernel_config.py
manually add the line:
c.InlineBackend.rc = {'figure.dpi':160}
rcParams references
https://matplotlib.org/3.1.1/tutorials/introductory/customizing.html
One away to set default plot size in matplotlib is:
from pylab import rcParams
rcParams['figure.figsize'] = 8, 6
where 8, 6 is the figure size you want to set.

how seaborn changes the behavior of pyplot by just importing the package?

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

How to set default matplotlib style?

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.

ggplot styles in Python

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.

Which is the recommended way to plot: matplotlib or pylab?

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.

Categories