Is there a way to make matplotlib
behave identically to R, or almost like R, in terms of plotting defaults? For example R treats its axes pretty differently from matplotlib
. The following histogram
has "floating axes" with outward ticks, such that there are no inner ticks (unlike matplotlib
) and the axes do not cross "near" the origin. Also, the histogram can "spillover" to values that are not marked by the tick - e.g. the x-axis ends at 3 but the histograms extends slightly beyond it. How can this be achieved automatically for all histograms in matplotlib
?
Related question: scatter plots and line plots have different default axes settings in R, for example:
There no inner ticks again and the ticks face outward. Also, the ticks start slightly after the origin point (where the y and x axes cross at the bottom left of the axes) and the ticks end slightly before the axes end. This way the labels of the lowest x-axis tick and lowest y-axis tick can't really cross, because there's a space between them and this gives the plots a very elegant clean look. Note that there's also considerably more space between the axes ticklabels and the ticks themselves.
Also, by default there are no ticks on the non-labeled x or y axes, meaning the y-axis on the left that is parallel to the labeled y-axis on the right has no ticks, and same for the x-axis, again removing clutter from the plots.
Is there a way to make matplotlib look like this? And in general to look by default as much as default R plots? I like matplotlib
a lot but I think the R defaults / out-of-the-box plotting behavior really have gotten things right and its default settings rarely lead to overlapping tick labels, clutter or squished data, so I would like the defaults to be as much like that as possible.
Setting spines in matplotlibrc explains why it is not possible to simply edit Matplotlib defaults to produce R-style histograms. For scatter plots, R style data-axis buffer in matplotlib and In matplotlib, how do you draw R-style axis ticks that point outward from the axes? show some defaults that can be changed to give a more R-ish look. Building off some of the other answers, the following function does a decent job of mimicking R's histogram style, assuming you've called
hist()
on yourAxes
instance withfacecolor='none'
.I would check out Bokeh which aims to "provide a compelling Python equivalent of ggplot in R". Example here
EDIT: Also check out Seaborn, which attempts to reproduce the visual style and syntax of ggplot2.
# # # # # #
EDIT 10/14/2013: For information, ggplot has now been implemented for python (built on matplotlib).
See this blog or go directly to the github page of the project for more information and examples.
# # # # # #
To my knowledge, there is no built-in solution in matplotlib that will directly give to your figures a similar look than the ones made with R.
Some packages, like mpltools, adds support for stylesheets using Matplotlib’s rc-parameters, and can help you to obtain a ggplot look (see the ggplot style for an example).
However, since everything can be tweaked in matplotlib, it might be easier for you to directly develop your own functions to achieve exactly what you want. As an example, below is a snippet that will allow you to easily customize the axes of any matplotlib plot.
EDIT: for non touching spines, see the function below which induces a 10 pts displacement of the spines (taken from this example on the matplotlib website).
For example, the code and the two plots below show you the default output from matplotib (on the left), and the output when the functions are called (on the right):
Of course, it will take time for you to figure out which parameters have to be tweaked in matplotlib to make your plots look exactly like the R ones, but I am not sure there are other options right now.
Here's a blog post you may be interested to read:
Plotting for Pandas GSoC2012
http://pandasplotting.blogspot.com/
The author forked pandas and built what looks like quite a lot of ggplot2-style grammar for pandas.
The pandas fork is here: https://github.com/orbitfold/pandas
Seems like meat of the code to make the R-influenced graphics is in a file called
rplot.py
which can be found in a branch in the repo.Link to the branch:
https://github.com/orbitfold/pandas/blob/rplot/pandas/tools/rplot.py
I thought this was really cool, but I can't figure out if this project is being maintained or not. The last commit was a while ago.
Edit 1 year later:
With
seaborn
, the example below becomes:Pretty dang easy.
Original post:
This blog post is the best I've seen so far. http://messymind.net/making-matplotlib-look-like-ggplot/
It doesn't focus on your standard R plots like you see in most of the "getting started"-type examples. Instead it tries to emulate the style of ggplot2, which seems to be nearly universally heralded as stylish and well-designed.
To get the axis spines like you see the in bar plot, try to follow one of the first few examples here: http://www.loria.fr/~rougier/coding/gallery/
Lastly, to get the axis tick marks pointing outward, you can edit your
matplotlibrc
files to sayxtick.direction : out
andytick.direction : out
.Combining these concepts together we get something like this:
The position of the spines can be specified a number of ways. If you run the code above in IPython, you can then do
axes.spines['bottom'].set_position?
to see all of your options.So yeah. It's not exactly trivial, but you can get close.
matplotlib >= 1.4 suports styles (and ggplot-style is build in):