1

I really liked this answer to another question regarding pairgrid but I'd like to have correlation coefficients on diagonal too rather than distplots. Unfortunately, when I simply pass the corrdot function to the map_diag I get following error:

import seaborn as sns
def corrdot(*args, **kwargs):
    corr_r = args[0].corr(args[1], 'pearson')
    corr_text = f"{corr_r:2.4f}".replace("0.", "0.")
    ax = plt.gca()
    ax.set_axis_off()
    marker_size = abs(corr_r) * 23000
    ax.scatter([.5], [.5], marker_size, [corr_r], alpha=0.6, cmap="coolwarm",
               vmin=-1, vmax=1, transform=ax.transAxes)
    font_size = abs(corr_r) * 40 + 5
    ax.annotate(corr_text, [.5, .5,],  xycoords="axes fraction",
                ha='center', va='center', fontsize=font_size)
    
sns.set(style='white', font_scale=1.6)
bulks = df_bulk_id
g = sns.PairGrid(bulks, aspect=1.4, diag_sharey=False)
g.map_lower(sns.regplot, lowess=True, ci=False, line_kws={'color': 'black'})
g.map_diag(corrdot)
g.map_upper(corrdot)

Error:

AttributeError: 'numpy.ndarray' object has no attribute 'corr'

How can I make it work?

I know it'll be 1 since its going to be correlated with itself but I want to show it rather than the distplot.

  • A correlation coefficient is a function of two vectors. The diagonal represents a single vector. What are you expecting to correlate it with? – mwaskom Sep 25 '20 at 13:32
  • I know it'll be 1 one but I want to show that rather than the distplot. Basically its correlation with itself according to matrix –  Sep 25 '20 at 13:37

0 Answers0