在循环中更新 pyplot 图例

去蓝

每次循环迭代时,我都试图向图例添加一个条目。abstr我想以某种方式更新循环中的传说,这样它会plt.plot他们,并更新给出的传说ab有什么办法可以做到这一点吗?谢谢!

plt.figure(figsize=(8.5, 4.5))
for a,b in zip(assets,buckets):
    df_temp = pd.read_excel(filename, sheet_name = a)
    df_temp = df_temp[['SPOT', b]]
    df_temp.dropna(inplace=True)
    df_temp_change = np.log(df_temp.values[:-1] / df_temp.values[1:])
    x1 = df_temp_change[:,0]
    x2 = df_temp_change[:,1]
    t_corr_vectors = threshold_corr(x1, x2, q_ts)
#    t_corr_vectors1 = threshold_corr(x1, x2, q_ts1)
#    t_corr_vectors2 = threshold_corr(x1, x2, q_ts2)
    plt.plot(q_ts, t_corr_vectors)
    plt.legend((t_corr_vectors),(a + "-" + b))
plt.title("Threshold Correlations")
plt.ylabel("Correlations")
plt.xlabel("Quantiles")
plt.savefig("TEST.jpg")
安德烈亚

您可以在 plot 函数中设置标签,然后在最后激活图例:

plt.figure(figsize=(8.5, 4.5))
for a,b in zip(assets,buckets):
    df_temp = pd.read_excel(filename, sheet_name = a)
    df_temp = df_temp[['SPOT', b]]
    df_temp.dropna(inplace=True)
    df_temp_change = np.log(df_temp.values[:-1] / df_temp.values[1:])
    x1 = df_temp_change[:,0]
    x2 = df_temp_change[:,1]
    t_corr_vectors = threshold_corr(x1, x2, q_ts)
#    t_corr_vectors1 = threshold_corr(x1, x2, q_ts1)
#    t_corr_vectors2 = threshold_corr(x1, x2, q_ts2)
    plt.plot(q_ts, t_corr_vectors, label=(a + "-" + b))
plt.legend()
plt.title("Threshold Correlations")
plt.ylabel("Correlations")
plt.xlabel("Quantiles")
plt.savefig("TEST.jpg")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章