如何在 Pandas 中绘制和标记多个自相关函数? [英] How plot and label multiple autocorrelation functions in Pandas?

查看:27
本文介绍了如何在 Pandas 中绘制和标记多个自相关函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个变量,我想在一张图上查看它们的自相关函数.我可以做这个.但问题是我不知道如何创建图例以便我知道哪个变量是哪个.

I have several variables and I want to look at their autocorrelation functions on one graph. I can do this. But the issue is I am not sure how to create a legend so that I know which variable is which.

这是我的数据的样子:

import pandas as pd 
from pandas.plotting import autocorrelation_plot
df = pd.DataFrame(data = {
    "Year": [y for y in range(1800, 2000)],
    "Var 1": [random.random() for i in range(200)], 
    "Var 2": [random.random() for i in range(200)], 
    "Var 3": [random.random() for i in range(200)]
})

df.set_index("Year")

现在这是我在一张图上绘制自相关函数的方法:

Now here's how I'd plot the autocorrelation functions on one graph:

for variable in df.columns:
    autocorrelation_plot(df[variable])

问题是没有图例,所以我不知道哪个变量是哪个.

The issue is that there's no legend, so I don't know which variable is which.

此外,autocorrelation_plot 没有 legend 参数.

Moreover, autocorrelation_plot does not have a legend argument.

推荐答案

我添加了一个标签变量来获取标签:

I have added a label variable to get the labels:

for variable in df.columns:
   ax = autocorrelation_plot(df[variable], label = variable)

股票数据

它奏效了

这篇关于如何在 Pandas 中绘制和标记多个自相关函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆