简体   繁体   English

使用 Altair 通过散点图 plot 上的均值绘制线性回归线

[英]Plotting a linear regression line through means on scatter plot using Altair

I am brand new to Altair and relatively new to matplotlib. The data set I am using relates to the PGA and golfers who won tournaments in 2017. I filtered my data to only include those players who had at least one win.我是 Altair 的新手,对 matplotlib 来说相对较新。我使用的数据集与 PGA 和 2017 年赢得锦标赛的高尔夫球手有关。我过滤了我的数据,只包括那些至少赢得过一场比赛的球员。 Furthermore, I am grouping players by their numbers of wins (3 subgroups total as the max number of wins was 3).此外,我根据获胜次数对玩家进行分组(总共 3 个子组,因为最大获胜次数为 3)。 The vertical axis consists of average driving shot distance of each player.纵轴由每个球员的平均开球距离组成。

I have created a scatter plot for my data but I would like to visualize and see if there is a positive trend line going through the mean driving distances for each group of players.我为我的数据创建了一个散点图 plot,但我想可视化并查看是否有一条正趋势线穿过每组玩家的平均行驶距离。

I know that the mean values for 1, 2, and 3 wins are 297.8040, 305.56, and 311.85 respectively.我知道 1、2 和 3 次获胜的平均值分别为 297.8040、305.56 和 311.85。

I know from matplotlib, you can define a line with linear regression form y=mx+b and then just plot with plt.plot, but I haven't been able to find a similar route within Altair.我从 matplotlib 知道,你可以用线性回归形式 y=mx+b 定义一条线,然后只用 plt.plot 定义 plot,但我无法在 Altair 中找到类似的路线。

Screen grab of current scatter plot output below当前散点的屏幕截图 plot output 下面

alt.Chart(source).mark_circle(size=250).encode(
    x=alt.X('Number of Wins', scale=Scale(domain=[1, 3], zero=False, padding=1, clamp=True)),
    y=alt.Y('Avg Driving Distance', scale=Scale(domain=[280, 325], zero=False, padding=50, clamp=True),  title="Average Driving Distance"),
    color='Player',
    opacity=alt.value(90)

).properties(
    width=700,
    height=700
    
).interactive()

下面的电流散点图

Upon further diving into the altair docs, I found a page related to fitting linear regressions lines and the code that helped me was chart + chart.transform_regression('x', 'y').mark_line()在深入研究 altair 文档后,我发现了一个与拟合线性回归线相关的页面,对我有帮助的代码是chart + chart.transform_regression('x', 'y').mark_line()

I was not aware altair allowed to you to basically compile multiple figures on top of one another by just using +我不知道 altair 允许您通过使用+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM