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

喜你入骨 提交于 2020-01-24 03:06:45

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/26134082/python-seaborn-facetgrid-is-it-possible-to-set-row-category-label-location-to-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!