在Python中使用带关键点的列表以获得袖扣中最合适的线条

2024-06-12 10:22:23 发布

您现在位置: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.

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

非常感谢您的帮助


Tags: importtrue参数initmodechartthislist
1条回答
网友
1楼 · 发布于 2024-06-12 10:22:23

我非常喜欢袖扣,但使用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使用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()

相关问题 更多 >