在循环中更新pyplot图例

2024-04-24 19:19:08 发布

您现在位置:Python中文网/ 问答频道 /正文

每次循环迭代时,我都尝试向图例中添加一个条目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")

Tags: dfthresholdplot条目pltchangetempvalues
1条回答
网友
1楼 · 发布于 2024-04-24 19:19:08

可以在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")

相关问题 更多 >