在 Python 中的袖扣中使用带键的列表以获得最佳拟合线

问题描述

我是尝试使用袖扣制作散点图的初学者。包含最佳拟合线的可选参数是 bestfit=True生成 this chart代码如下所示:

enter image description here

import pandas as pd 
from plotly.offline import iplot,init_notebook_mode
import cufflinks
cufflinks.go_offline(connected=True)
init_notebook_mode(connected=True)
    
df = pd.read_csv('https://raw.githubusercontent.com/inferentialthinking/inferentialthinking.github.io/master/data/nba2013.csv')
    
df.iplot(
        
        z='Weight',x='Age in 2013',y='Weight',kind='scatter',mode='markers',xTitle='Age',yTitle="Weight",title="NBA players' weight and age",text='Name',theme='solar',bestfit=True
        #,categories='Position'
        
            )

但是,当我添加参数 categories='Position'(在本例中删除“#”)以创建颜色分类(将球员分为后卫、中锋和前锋)时,最佳拟合线消失了。 See chart of this here. 我没有收到任何错误消息,只是没有最佳拟合线了。

袖扣有助于最适合的论证状态:

bestfit : boolean or list
            If True then a best fit line will be generated for 
            all columns. 
            If list then a best fit line will be generated for 
            each key on the list.

我想为三个类别中的每一个获得一条最佳拟合线(即三个最佳拟合线)。我不明白如何使用列表为“列表中的每个键”生成最佳拟合线。在这种情况下,如果可能的话,如果有人能解释如何做到这一点就太好了?

非常感谢任何帮助!

解决方法

我真的很喜欢袖扣,但你在这里的目标是使用 plotly express 更容易:

fig = px.scatter(df,x = 'Age in 2013',y = 'Height',size = 'Weight',template = 'plotly_dark',color_discrete_sequence = colors[1:],color = 'Position',trendline = 'ols',title = 'NBA Players weight and age')

这种方法在很多方面类似于袖扣。唯一真正的看法是 px.scatter 使用 size,而 cufflinks 使用 z。当然,px.scatter 使用 Position 参数为 color 的每个子类别生成趋势线。

enter image description here

# imports
import pandas as pd
import plotly.express as px
import plotly.io as pio

# data
#df = px.data.stocks()
df = pd.read_csv('https://raw.githubusercontent.com/inferentialthinking/inferentialthinking.github.io/master/data/nba2013.csv')

colors = px.colors.qualitative.T10

# plotly
fig = px.scatter(df,title = 'NBA Players weight and age')
fig.show()