我正在使用seaborn pairplot针对因变量绘制几个自变量.
import seaborn as sns
iris = sns.load_dataset("iris")
x_vars = ['sepal_length', 'sepal_width', 'petal_length']
y_vars = ['petal_width']
pp = sns.pairplot(data=iris, x_vars=x_vars, y_vars=y_vars)
现在,我想向第二和第三子图添加y轴刻度和标签.
添加y轴标签很容易:
pp.set(ylabel='petal_width')
但是我无法终生弄清楚如何显示y_ticklabels.
像:
pp.set(yticklabels=np.arange(-0.5, 3.01, 0.5))
要么:
for i in range(3):
ax = pp.axes[0,i]
ax.set_yticks(np.arange(-0.5, 3.01, 0.5))
ax.set_yticklabels(np.arange(-0.5, 3.01, 0.5))
ax.set_visible(True)
没关系.
解决方法:
只需将yticklabel变回可见,就可以在所需的子图中进行操作了:
import seaborn as sns
iris = sns.load_dataset("iris")
x_vars = ['sepal_length', 'sepal_width', 'petal_length']
y_vars = ['petal_width']
pp = sns.pairplot(data=iris, x_vars=x_vars, y_vars=y_vars)
_ = plt.setp(pp.axes[0,1].get_yticklabels(), visible=True) #changing the 2nd plot
_ = …这里是为了抑制交互式环境中不必要的打印输出.