问题
How can I put variable names on the diagonal of a Seaborn PairGrid, like this from the PerformanceAnalytics R package's chart.Correlation function?
I imagine this involves a custom function to pass to map_diag, but don't know how to extract variable names for it.
For example, this shows the entire array:
import matplotlib.pyplot as plt
import seaborn as sns
iris = sns.load_dataset('iris')
def diagfunc(x, **kws):
ax = plt.gca()
ax.annotate(x, xy=(.1, .9), xycoords=ax.transAxes)
sns.PairGrid(iris).map_diag(diagfunc)
回答1:
The original solution here does not work anymore with seaborn >= 0.9. This is due to the change made in #1464, such that from now on the data will be passed as numpy array, which do not have a name, instead of a pandas series.
A workaround would be to assume that the order of the axes in the pairgrid follows the order of columns in the dataframe. Then using that order one can iterate over the column names.
import matplotlib.pyplot as plt
import seaborn as sns
df = sns.load_dataset('iris')
it = iter(list(df.columns))
def diagfunc(*args, **kws):
plt.gca().annotate(next(it), xy=(0, 1), xytext=(5,-5), ha="left", va="top",
xycoords=plt.gca().transAxes, textcoords="offset points")
sns.PairGrid(df).map_diag(diagfunc)
plt.show()
回答2:
Note: this no longer works.
Use x.name in the map_diag function, which is passed the series of each column.
import matplotlib.pyplot as plt
import seaborn as sns
iris = sns.load_dataset('iris')
def diagfunc(x, **kws):
ax = plt.gca()
ax.annotate(x.name, xy=(0.05, 0.9), xycoords=ax.transAxes)
sns.PairGrid(iris).map_diag(diagfunc)
To also remove the normal axis titles, follow Remove axis titles from Seaborn PairGrid and add:
for ax in g.axes.flatten():
ax.set_ylabel('')
ax.set_xlabel('')
来源:https://stackoverflow.com/questions/48145811/put-variable-names-on-diagonal-of-seaborn-pairgrid