Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python seaborn facetGrid: Is it possible to set row category label location to the left

When using Seaborn facetGrid plots. Is it possible to set the row variable label to the left (eg. as the first line of the two-line sub-plots y axes label)?

The default location is on the top as part of the sub-plot title. unfortunately the combined text sometimes gets too long to legitably fit into that crowded space. Then I tried to use margin_titles = True option when instantiate the facetGrid object. But in this case, the row variable label is outside to the right of the legend, which can be awkwardly too far from the chart.

So the possible easy ways to improve the aesthetic in my two-cent worth of thought:

  1. Move the margin title inside the legend when margin_titles = True and legend_out=True
  2. Allow the row variable label to be displayed on the left before the y axis label.
  3. Other ideas?

Sorry, haven't accumulated enough points to be able to add a chart example.

like image 662
user3287545 Avatar asked Oct 01 '14 04:10

user3287545


People also ask

What does FacetGrid do in Seaborn?

FacetGrid object takes a dataframe as input and the names of the variables that will form the row, column, or hue dimensions of the grid. The variables should be categorical and the data at each level of the variable will be used for a facet along that axis.

How do you put two Seaborn plots together?

To create two graphs, we can use nrows=1, ncols=2 with figure size (7, 7). Create a data frame with keys, col1 and col2, using Pandas. Use countplot() to show the counts of observations in each categorical bin using bars. Adjust the padding between and around the subplots.

Which plots can be plotted using faceted grids?

The plots it produces are often called “lattice”, “trellis”, or “small-multiple” graphics. It can also represent levels of a third variable with the hue parameter, which plots different subsets of data in different colors.


1 Answers

Sure thing! Enumerating over the axes seems to work just fine.

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style='ticks', color_codes=True)

tips = sns.load_dataset('tips')

g = sns.FacetGrid(tips, col='time', row='sex')
g = g.map(plt.scatter, 'total_bill', 'tip', edgecolor='w')

for i, axes_row in enumerate(g.axes):
    for j, axes_col in enumerate(axes_row):
        row, col = axes_col.get_title().split('|')

        if i == 0:
            axes_col.set_title(col.strip())
        else:
            axes_col.set_title('')

        if j == 0:
            ylabel = axes_col.get_ylabel()
            axes_col.set_ylabel(row.strip() + ' | ' + ylabel)

plt.show()

enter image description here

like image 53
John Karasinski Avatar answered Oct 29 '22 21:10

John Karasinski