Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 892
Josh Avatar asked Mar 20 '14 19:03

Josh


People also ask

What is style use Ggplot in Python?

A key feature of mpltools is the ability to set “styles”—essentially, stylesheets that are similar to matplotlibrc files. This example demonstrates the “ggplot” style, which adjusts the style to emulate ggplot (a popular plotting package for R). These settings were shamelessly stolen from [1].

Can I use ggplot2 in Python?

Using ggplot in Python allows you to build visualizations incrementally, first focusing on your data and then adding and tuning components to improve its graphical representation.

What is Ggplot in Python matplotlib?

ggplot has a special technique called faceting that allows to split one plot into multiple plots based on a factor included in the dataset. We will use it to make one plot for a time series for each species.

What is style use (' Fivethirtyeight ') in Python?

Another example of a style is from fivethirtyeight: style. use('fivethirtyeight') You can see all of the available styles you currently have by doing: print(plt. style.


2 Answers

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.

like image 177
joris Avatar answered Sep 17 '22 13:09

joris


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) 
like image 20
Jan Katins Avatar answered Sep 20 '22 13:09

Jan Katins