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

标签 python pandas list plotly

我是一名尝试使用袖扣制作散点图的初学者。包含最佳拟合线的可选参数是 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')

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

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, 
                 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')
fig.show()

关于python - 在 Python 的袖扣中使用带键的列表以获得最佳拟合线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66515597/

相关文章:

python - Scipy 中的高性能计算,具有独立应用于大量输入的数值函数

python - itertools.chain.from_iterable 适用于嵌套数字列表,但不适用于字符串列表?

python - Python中列表列表中的列表中的值的平均值

list - Scala 迭代列表

python - str.translate 给出 TypeError - Translate 接受一个参数(给定 2 个),在 Python 2 中工作

Python如何在列表中进行搜索

python - 在Python中使用Rpy2更改ggplot2中的因子顺序

python - 从 Pandas 数据框文本列获取数组的有效方法

python - 如何在 Pandas 中将单级 df 合并为多级 df?

python - 列表继承 : "extend" vs "+" vs "+="