简体   繁体   中英

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.

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()

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM