1
votes

I'm plotting pandas data using a seaborn factorplot. The code is as follows:

import seaborn
import numpy as np
import pandas as pd

seaborn.set_style("white")

fg = seaborn.factorplot(x='Group', y='val', hue='Mean', 
                        col='Sex', data=data, kind='bar', ci=68, row='Split', x_order=['Group 1', 'Group 2'],
                        row_order=['Poor','Good'], col_order=['Male', 'Female'], sharex=False, sharey=False, 
                        palette='deep', legend_out=False
                       )

(fg.set_axis_labels("", "val")
  .set_titles("{row_name} - {col_name}")
  .set(ylim=(0, 300))
  .despine(left=True)
) 

Here is an example dataframe:

groups = ('Group 1', 'Group 2')
sexes = ('Male', 'Female')
means = ('Low', 'High')
split = ('Poor', 'Good')
index = pd.MultiIndex.from_product(
    [groups, sexes, means, split], 
   names=['Group', 'Sex', 'Mean', 'Split']
)

values = np.random.randint(low=20, high=100, size=len(index))
data = pd.DataFrame(data={'val': values}, index=index).reset_index()

I've looked to see what aesthetic options seaborn has and there are a few that I cannot find reference to:

  • This creates a 2x2 grid of 4 plots. How do I adjust the spacing between each of the plots? Right now, everything is too close together

  • How do I individually set the font size for the seperate labels? I'd like some axis/categorical labels to be bigger than others

  • How do I completely remove the legend title?

  • With a 2x2 grid of plots, is it possible to put the 2 plots in the same row onto the same plot? i.e. connect the x axis, remove the y axis on the right plot

  • Is it possible to save plots to a vector based file format?

1
can you provide a snippet of your DataFrame to run some tests?Diziet Asahi

1 Answers

1
votes
fg = seaborn.factorplot(x='Group', y='val', hue='Mean', 
                        col='Sex', data=data, kind='bar',
                        ci=68, row='Split',
                        order=['Group 1', 'Group 2'],
                        row_order=['Poor','Good'],
                        col_order=['Male', 'Female'],
                        sharex=False, sharey="row", 
                        palette='deep', legend_out=False
                       )

(fg.set_axis_labels("", "val")
  .set_titles("{row_name} - {col_name}")
  .set(ylim=(0, 300))
  .despine(left=True)
)

# Increase space between rows and remove space between cols
fg.fig.subplots_adjust(hspace=.4, wspace=0)

# Change some label sizes
fg.axes[0, 0].title.set(size=16)
fg.axes[1, 0].yaxis.label.set(size=16)
plt.setp(fg.axes[0, 1].get_xticklabels(), size=16)

# Remove the legend title
fg.axes[0, 0].legend()

# Save to a vector format
fg.savefig("figure.svg")

enter image description here

Note that in addition to the extra lines, I added sharey="row" in the call to factorplot which removes the y axis ticks