3 回答

TA貢獻1807條經(jīng)驗 獲得超9個贊
我添加了一個標簽變量來獲取標簽:
for variable in df.columns: ax = autocorrelation_plot(df[variable], label = variable)
它奏效了

TA貢獻1864條經(jīng)驗 獲得超6個贊
試試這個代碼:
for variable in df.columns:
ax = autocorrelation_plot(df[variable])
ax.legend(ax.get_lines())
autocorrelation_plot返回一個類型的對象,AxesSubplot它允許您像使用 matplotlib 一樣操作圖形。因此,要添加圖例,您只需將函數(shù)剛剛繪制的線條作為參數(shù)傳遞。
例如,使用此代碼,我打印每條打印行的顏色,然后更改行的標簽,添加字符串variable:
i=0
for variable in df.columns:
ax = autocorrelation_plot(df[variable])
print(variable, ax.get_lines()[-1].get_color())
for k in range(i, len(ax.get_lines())):
ax.get_lines()[k].set_label(f'{k}_{variable}')
i+=6
ax.legend(ax.get_lines())

TA貢獻1802條經(jīng)驗 獲得超10個贊
我即興創(chuàng)作了來自@Massifox 的答案,我能夠為自相關(guān)圖定制一個圖例
from matplotlib.lines import Line2D
plot_color = []
for variable in df.columns:
ax = autocorrelation_plot(df[variable])
plot_color.append((ax.get_lines()[-1].get_color()))
custom_lines = [Line2D([0],[0], color=plot_color [0], lw=2),
Line2D([0],[0], color=plot_color [1], lw=2),
Line2D([0],[0], color=plot_color [2], lw=2)]
ax.legend(custom_lines, ['label1', 'label2', 'label3'])
添加回答
舉報