def show_scatter_and_results(df):
"""Creates a plot which shows both the plot and the statistical summary
Args:
df (pd.DataFrame): The data set to plot
labels (List[str]): The labels to use for
"""
plt.figure(figsize=(12, 5))
sns.regplot("x", y="y", data=df, ci=None, fit_reg=False,
scatter_kws={"s": 50, "alpha": 0.7, "color": "black"})
plt.xlim(-5, 105)
plt.ylim(-5, 105)
plt.tight_layout()
res = get_values(df)
fs = 30
y_off = -5
labels = ("X Mean", "Y Mean", "X SD", "Y SD", "Corr.")
max_label_length = max([len(l) for l in labels])
# If `max_label_length = 10`, this string will be "{:<10}: {:0.9f}", then we
# can pull the `.format` method for that string to reduce typing it
# repeatedly
formatter = '{{:<{pad}}}: {{:0.9f}}'.format(pad=max_label_length).format
corr_formatter = '{{:<{pad}}}: {{:+.9f}}'.format(pad=max_label_length).format
opts = dict(fontsize=fs, alpha=0.3)
plt.text(110, y_off + 80, formatter(labels[0], res[0])[:-2], **opts)
plt.text(110, y_off + 65, formatter(labels[1], res[1])[:-2], **opts)
plt.text(110, y_off + 50, formatter(labels[2], res[2])[:-2], **opts)
plt.text(110, y_off + 35, formatter(labels[3], res[3])[:-2], **opts)
plt.text(110, y_off + 20, corr_formatter(labels[4], res[4], pad=max_label_length)[:-2], **opts)
opts['alpha'] = 1
plt.text(110, y_off + 80, formatter(labels[0], res[0])[:-7], **opts)
plt.text(110, y_off + 65, formatter(labels[1], res[1])[:-7], **opts)
plt.text(110, y_off + 50, formatter(labels[2], res[2])[:-7], **opts)
plt.text(110, y_off + 35, formatter(labels[3], res[3])[:-7], **opts)
plt.text(110, y_off + 20, corr_formatter(labels[4], res[4], pad=max_label_length)[:-7], **opts)
plt.tight_layout(rect=[0, 0, 0.57, 1])
评论列表
文章目录