Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access axes object in seaborn lmplot [duplicate]

Most seaborn plotting functions (e.g. seaborn.barplot, seaborn.regplot) return a matplotlib.pyplot.axes when called, so that you can use this object to further customize the plot as you see fit.

However, I wanted to create an seaborn.lmplot, which doesn't return the axes object. After digging through the documentation of both seaborn.lmplot and seaborn.FacetGrid (which lmplot uses in it's backend), I found no way of accessing the underlying axes objects. Moreover, while most other seaborn functions allow you to pass your own axes as a parameter on which they will draw the plot on, lmplot doesn't.

One thing I thought of is using plt.gca(), but that only returns the last axes object of the grid.

Is there any way of accessing the axes objects in seaborn.lmplot or seaborn.FacetGrid?

like image 977
nazz Avatar asked Sep 08 '18 11:09

nazz


People also ask

Which parameter of the Seaborn method Lmplot () allows for wrapping column variables into multiple rows?

col_wrap : (optional) This parameter is of int type, “Wrap” the column variable at this width, so that the column facets span multiple rows.

What does SNS Implot () perform in Seaborn library?

The lineplot (lmplot) is one of the most basic plots. It shows a line on a 2 dimensional plane. You can plot it with seaborn or matlotlib depending on your preference. The examples below use seaborn to create the plots, but matplotlib to show.


1 Answers

Yes, you can access the matplotlib.pyplot.axes object like this:

import seaborn as sns
lm = sns.lmplot(...)  # draw a grid of plots
ax = lm.axes  # access a grid of 'axes' objects

Here, ax is an array containing all axes objects in the subplot. You can access each one like this:

ax.shape  # see the shape of the array containing the 'axes' objects
ax[0, 0]  # the top-left (first) subplot 
ax[i, j]  # the subplot on the i-th row of the j-th column

If there is only one subplot you can either access it as I showed above (with ax[0, 0]) or as you said in your question through (plt.gca())

like image 139
Djib2011 Avatar answered Sep 25 '22 10:09

Djib2011