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?
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.
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.
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.
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()
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With