简体   繁体   中英

How to change the positions of subplot titles and axis labels in Seaborn FacetGrid?

I am trying to plot a polar plot using Seaborn's facetGrid, similar to what is detailed on seaborn's gallery I am using the following code:

sns.set(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1.25)

# Set up a grid of axes with a polar projection
g = sns.FacetGrid(df_total, col="Construct", hue="Run", col_wrap=5,    subplot_kws=dict(projection='polar'), size=5, sharex=False, sharey=False, despine=False)

# Draw a scatterplot onto each axes in the grid
g.map(plt.plot, 'Rad', ''y axis label', marker=".", ms=3,   ls='None').set_titles("{col_name}")

plt.savefig('./image.pdf')

Which with my data gives the following:

在此处输入图片说明

I want to keep this organisation of 5 plots per line.

The problem is that the title of each subplot overlap with the values of the ticks, same for the y axis label.

Is there a way to prevent this behaviour? Can I somehow shift the titles slightly above their current position and can I shift the y axis labels slightly on the left of their current position?

Many thanks in advance!

EDIT: This is not a duplicate of this SO as the problem was that the title of one subplot overlapped with the axis label of another subplot.

Here my problem is that the title of one subplot overlaps with the ticks label of the same subplot and similarly the axis label overlaps with the ticks label of the same subplot.

I also would like to add that I do not care that they overlap on my jupyter notebook (as it as been created with it), however I want the final saved image with no overlap, so perhaps there is something I need to do to save the image in a slightly different format to avoid that, but I don't know what (I am only using plt.savefig to save it).

EDIT 2: If someone would like to reproduce the problem here is a minimal example:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

sns.set()
sns.set(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1.5)
# Generate an example radial datast
r = np.linspace(0, 10000, num=100)
df = pd.DataFrame({'label': r, 'slow': r, 'medium-slow': 1 * r, 'medium': 2 * r, 'medium-fast': 3 * r, 'fast': 4 * r})

# Convert the dataframe to long-form or "tidy" format
df = pd.melt(df, id_vars=['label'], var_name='speed', value_name='theta')

# Set up a grid of axes with a polar projection
g = sns.FacetGrid(df, col="speed", hue="speed",
                  subplot_kws=dict(projection='polar'), size=4.5, col_wrap=5,
                  sharex=False, sharey=False, despine=False)

# Draw a scatterplot onto each axes in the grid
g.map(plt.scatter, "theta", "label")

plt.savefig('./image.png')
plt.show()

Which gives the following image in which the titles are not as bad as in my original problem (but still some overlap) and the label on the left hand side overlap completely. 在此处输入图片说明

In order to move the title a bit higher you can set at new position,

ax.title.set_position([.5, 1.1])

In order to move the ylabel a little further left, you can add some padding

ax.yaxis.labelpad = 25

To do this for the axes of the facetgrid, you'd do:

for ax in g.axes:
    ax.title.set_position([.5, 1.1])
    ax.yaxis.labelpad = 25

在此SO 问题中 ImportanceOfBeingErnest提供的答案可能会有所帮助。

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