Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase space between rows on FacetGrid plot

I've the following code which creates the plot you can see in the picture:

g = sns.FacetGrid(data, col="Provincia",col_wrap=6,size=2.5)
g.map(sns.barplot, "Anio", "Diff");
g.set_axis_labels("Año", "Porcentaje de aumento");

for ax in g.axes.flat:
    _ = plt.setp(ax.get_yticklabels(), visible=True)
    _ = plt.setp(ax.get_xticklabels(), visible=False)
    _ = plt.setp(ax.get_xticklabels()[0], visible=True)    
    _ = plt.setp(ax.get_xticklabels()[-1], visible=True)

The problem, as you can see in the picture, is that the x ticks collapse with the col name below. What is the proper way to increase this space in order to fix this?

output

like image 871
Rod0n Avatar asked Apr 27 '17 22:04

Rod0n


People also ask

How do you plot FacetGrid?

Plotting Small Multiples of Data SubsetsA FacetGrid can be drawn with up to three dimensions − row, col, and hue. The first two have obvious correspondence with the resulting array of axes; think of the hue variable as a third dimension along a depth axis, where different levels are plotted with different colors.

What does SNS FacetGrid do?

FacetGrid() : FacetGrid class helps in visualizing distribution of one variable as well as the relationship between multiple variables separately within subsets of your dataset using multiple panels.

What is Col_wrap?

col_wrapint. “Wrap” the column variable at this width, so that the column facets span multiple rows. Incompatible with a row facet. share{x,y}bool, 'col', or 'row' optional. If true, the facets will share y axes across columns and/or x axes across rows.


1 Answers

tight layout

You can use tight_layout to automatically adjust the spacings

g.fig.tight_layout()

or, if you have matplotlib.pyplot imported as plt,

plt.tight_layout()

subplots adjust

You can use plt.subplots_adjust to manually set the spacings between subplots,

plt.subplots_adjust(hspace=0.4, wspace=0.4)

where hspace is the space in height, and wspace is the space width direction.

gridspec keyword arguments

You could also use gridspec_kws in the FacetGrid initialization,

g = sns.FacetGrid(data, ... , gridspec_kws={"wspace":0.4})

However, this can only be used if col_wrap is not set. (So it might not be an option in the particular case from the question).

like image 126
ImportanceOfBeingErnest Avatar answered Sep 19 '22 00:09

ImportanceOfBeingErnest