使用Seaborn在散点图的x轴上显示索引标签

时间:2018-07-22 13:25:01

标签: python seaborn labels

我与Seaborn创建了一个散点图,我在想,如果我能在x轴上显示观测值的索引标签(旋转90度),这将有助于读者解释该图。但是我不知道该怎么做。

我的数据:

                      Response8  Nulls_prevalence
Employment_Info_1     0.001348              0.00
Employment_Info_4    -0.000049              0.11
Medical_History_1     0.078445              0.15
Employment_Info_6     0.003095              0.18
Family_Hist_4        -0.119424              0.32
Insurance_History_5  -0.003648              0.43
Family_Hist_2        -0.004765              0.48
Family_Hist_3        -0.003509              0.58
Family_Hist_5        -0.003889              0.70
Medical_History_15    0.263364              0.75
Medical_History_24    0.112906              0.94
Medical_History_32    0.485493              0.98
Medical_History_10    0.203842              0.99

我的代码:

import pandas as pd
import seaborn as sns

sns.regplot(x = 'Nulls_prevalence', y='Response8' , data = plot_data8, fit_reg=False)

plt.title('Response8:  Nulls_prevalence of Predictors vs. Correlation with Target')

plt.xlabel('Nulls Prevalence of Predictor')

plt.ylabel('Correlation of Predictor with Target')

plt.tight_layout()

plt.show()

输出:

enter image description here

我试了一下,但我无法使所有索引标签都出现(在x轴上只出现了一个子集)。

您的建议将不胜感激。

2 个答案:

答案 0 :(得分:0)

我认为您要问的是seaborn的rugplot功能的功能。

import pandas as pd
import seaborn as sns

ax = sns.regplot(x = 'Nulls_prevalence', y='Response8' , data = plot_data8, fit_reg=False)

sns.rugplot(plot_data8['Nulls_prevalence'], ax=ax) # Don't forget to pass the axis from regplot

plt.title('Response8:  Nulls_prevalence of Predictors vs. Correlation with Target')

plt.xlabel('Nulls Prevalence of Predictor')

plt.ylabel('Correlation of Predictor with Target')

plt.tight_layout()

plt.show()

答案 1 :(得分:0)

我认为您需要plt.xticks

import pandas as pd
import seaborn as sns

plt.figure(figsize=(15,8)
sns.regplot(x = 'Nulls_prevalence', y='Response8' , data = plot_data8, fit_reg=False)

plt.title('Response8:  Nulls_prevalence of Predictors vs. Correlation with Target')

plt.xlabel('Nulls Prevalence of Predictor')

plt.ylabel('Correlation of Predictor with Target')

plt.tight_layout()

#plt.xticks line
plt.xticks(plot_data8['Nulls_prevalence'], rotation=90)

plt.show()

输出:

enter image description here