Seaborn PairGrid:显示每个子图的轴标签 [英] Seaborn PairGrid: show axes labels for each subplot

查看:87
本文介绍了Seaborn PairGrid:显示每个子图的轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以轻松地为Seaborn对图中的每个子图添加轴标签?这与此

Is there a way that I can easily add the axes labels for each of the subplots in Seaborn pair plot? This is related to this question but instead of adding the tick labels I want to add the axes labels as the pairplot I am having is 9*9 and I dont want to scroll down every time to check the column name.

I was hoping that it would be some thing easy like

for ax in g.axes.flat:
    _ = plt.setp(ax.get_ylabels(), visible=True)
    _ = plt.setp(ax.get_xlabels(), visible=True)

解决方案

You first need to get all the labels from the axes (e.g. ax.xaxis.get_label_text()) and the set the label text (ax.xaxis.set_label_text()).

I've used a for loop and i, j indexing here. Its possible there's a cleaner vectorised way to do this, but at least it works.

Using the iris sample dataset from seaborn:

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

iris = sns.load_dataset("iris")

g = sns.PairGrid(iris)
g = g.map(plt.scatter)

xlabels,ylabels = [],[]

for ax in g.axes[-1,:]:
    xlabel = ax.xaxis.get_label_text()
    xlabels.append(xlabel)
for ax in g.axes[:,0]:
    ylabel = ax.yaxis.get_label_text()
    ylabels.append(ylabel)

for i in range(len(xlabels)):
    for j in range(len(ylabels)):
        g.axes[j,i].xaxis.set_label_text(xlabels[i])
        g.axes[j,i].yaxis.set_label_text(ylabels[j])

plt.show()

这篇关于Seaborn PairGrid:显示每个子图的轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆