ggplot styles in Python - 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.

Related

The seaborn styles shipped by Matplotlib are deprecated since 3.6

The seaborn styles shipped by Matplotlib are deprecated since 3.6,
as they no longer correspond to the styles shipped by seaborn.
However, they will remain available as 'seaborn-v0_8-<style>'. Alternatively, directly use the seaborn API instead.
I have tried this:
# use seaborn style
plt.style.use("seaborn")
but it is deprecated, and I want to remove this warning when I use the cmd in windows
This warning is telling you that seaborn styles in matplotlib do not match current seaborn styles, since the latest have been updated.
This is why you should set the style as follow:
plt.style.use("seaborn-v0_8")
You can specify a theme by replacing <style> with one the following:
white
dark
whitegrid
darkgrid
ticks
Just like this:
plt.style.use("seaborn-v0_8-whitegrid")
Alternatively, if you want to use the latest seaborn styles, use their library directly
I can confirm that adding the version to the use function, just as Tranbi described, works and removes the warning.
From the additional themes the one called "darkgrid" is almost the same as if I do not specify any.

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.

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.

Matplotlib turn off antialias for text in plot?

Is there any way to turn off antialias for all text in a plot, especially the ticklabels?
It seems this is not possible. Some classes such as Line2D have a "set_antialiased" method, but Text lacks this. I suggest you file a feature request on the Sourceforge tracker, and send an email to the matplotlib mailing list mentioning the request.
Did you try playing with the font/text properties of matplotlibrc ? That would be the first thing to try, I think (see here for examples: http://matplotlib.sourceforge.net/users/customizing.html)
I believe the anti-aliasing of Text objects is up to the font engine being used. It seems the freetype2 fonts support this.
Not sure if it already existed back in 2010, but I had the same issue and found that matplotlib has a text.antialiased parameter that applies to the tick labels too. Tested with the agg and cairo backends:
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['text.antialiased'] = False
plt.plot([0,1,2], [3,4,5])

Categories