Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib axis for FacetGrid

Why https://seaborn.pydata.org/generated/seaborn.FacetGrid.html

does not have ax argument that accepts matplotlib axis like other graphs

e.g., https://seaborn.pydata.org/generated/seaborn.distplot.html?

As I need to set the matplotlib axis for FacetGrid to bring in customized style.

like image 507
william007 Avatar asked Oct 21 '25 11:10

william007


1 Answers

FaceGrid makes the grid, so you cannot pass a grid to it. The individual plots are stored inside, and can be manipulated like in the code below. Also note that that Axes and Axis are different in Matplotlib. The code below shows a way to obtain both Axes and Axis:

import seaborn as sns
import pandas as pd

#dummy data
data= pd.DataFrame(data={'a':np.random.randint(0,2, 100),
                         'b':np.random.rand(100),
                         'c':np.random.rand(100)})

# make facetgrid and store in variable
g = sns.FacetGrid(data, col='a') # make facetgrid with 2 columns, because there are two distinct values in column a of data
g.map(plt.scatter, 'b', 'c') # map function to grid

# the individual axes of the grid are stored in g.
# you can access and edit them like so:

for ax in g.axes[0]:
    ax.set_ylabel('test')
    ax.set_ylim(0,1.5)
    ax.set_title('title')
    # now you can obtain the axis from one of the axes
    x_axis = ax.get_xaxis()

enter image description here

like image 130
warped Avatar answered Oct 23 '25 00:10

warped



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!