且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用公共轴绘制两个变量的仓位相关性

更新时间:2023-02-26 18:40:06

我已经或多或少地提出了解决方案.在此处实施类似于的操作:

I've more or less arrived at a solution. Implementing something similar to what was used here I have:

nbins = 20
df['bins'] = pd.qcut(df['x'], q=nbins)
plotdatadf = df.groupby('bins')[['y1', 'y2']].corr().iloc[0::2, -1]

这为我提供了一个数据框,每个仓的相关系数分别为 y1 y2 ,其中,仓沿 x 平均分配就每个箱的观察而言.

This provides me with a data frame with a correlation coefficient of y1 and y2 for each bin, where bins are evenly divided along x in terms of observations per bin.

我现在可以返回到先前的数据帧,并使用这些相关值添加原始长度的另一列,条件是 if bin [1]然后corr = corr [1] 类型的复制.然后可以将此列绘制为y,而将我已经存在的x绘制为折线图.

I can now go back to my previous dataframe and add another column of the original length with these correlation values, conditional on if bin[1] then corr = corr[1]-type copying. This column can then be plotted as y against my already existing x as a line plot.