简体   繁体   English

如何在每次迭代的图形标题中打印 pearsonr 循环的结果

[英]How to print the result of pearsonr loop in the graph title for each iteration

I have this code where the loop iterates through PRDCT column then calculates the p and r value, and creates a graph for each unique product code:我有这段代码,其中循环遍历 PRDCT 列,然后计算 p 和 r 值,并为每个唯一的产品代码创建一个图表:

for prd in df_final.PRDCT.unique():
    df_tmp = df_final[df_final.PRDCT== prd].reset_index().copy()
    coeff, p = pearsonr(df_tmp['PRDCT_mean'], np.arange(0,len(df_tmp['PRDCT_mean'])))
    plt.figure(figsize = (15,6))
    plt.plot(df_tmp['Month'],df_tmp['PRDCT_mean'], marker="o")
    plt.title(prd, fontsize=18)
    plt.ylabel('PRDCT_mean')
    plt.xlabel('Month')
    plt.grid(True)
    plt.ylim((-60,60))
    plt.xticks(rotation= 'vertical',size=8)
    plt.show()

Question 1 : How can I show the respective coefficient value of each unique product code beside the graph title of the each product?问题1:如何在每个产品的图形标题旁边显示每个唯一产品代码的各自系数值?

Question 2 : How can I save the result of each pearsonr P and r value that takes place in for each iteration seperately?问题 2:如何分别保存每次迭代中发生的每个 pearsonr P 和 r 值的结果?

Prefer these actions to include in the same code if possible如果可能,希望将这些操作包含在同一代码中

Thanks in adv感谢广告

Consider creating a defined method that handles all steps: builds plot, concatenates string statistics to title, and returns statistics.考虑创建一个处理所有步骤的已定义方法:构建绘图,将字符串统计信息连接到标题,并返回统计信息。 Then create a dictionary via comprehension using DataFrame.groupby .然后使用DataFrame.groupby通过理解创建字典。

def run_plot_save_stats(prd, df_tmp):
    df_tmp = df_tmp.reset_index().copy()
    coeff, p = pearsonr(df_tmp['PRDCT_mean'], np.arange(0,len(df_tmp['PRDCT_mean'])))
    title = f"Product: {prd} - pearson coeff: {coeff.round(4)} p-value: {p.round(4)}"

    plt.figure(figsize = (15,6))
    plt.plot(df_tmp['Month'],df_tmp['PRDCT_mean'], marker="o")
    plt.title(title, fontsize=18)
    plt.ylabel('PRDCT_mean')
    plt.xlabel('Month')
    plt.grid(True)
    plt.ylim((-60,60))
    plt.xticks(rotation= 'vertical',size=8)
    plt.show()

    return {"pearson": coeff, "p_value": p}

prod_stats_dict = {
    grp: run_plot_save_stats(grp, df) for grp, df in df_final.groupby("PRDCT")
}

prod_stats_dict["product1"]["pearson"]
prod_stats_dict["product1"]["p_value"]
prod_stats_dict["product2"]["pearson"]
prod_stats_dict["product2"]["p_value"]
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM