亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據 pandas 數據框向散點圖添加過濾器

根據 pandas 數據框向散點圖添加過濾器

繁花如伊 2022-06-14 16:48:53
我假裝使用以下數據框的過濾器制作散點圖(代表整個賽季的球員、球隊和賽季,并計算籃球隊球員所做的輔助和非輔助點):player          team_name       season          assisted    notassistedA. DANRIDGE     NACIONAL        Season_17_18    130         445A. DANRIDGE     NACIONAL        Season_18_19    132         382D. ROBINSON     TROUVILLE       Season_18_19    89          286D. DAVIS        AGUADA          Season_18_19    101         281E. BATISTA      WELCOME         Season_17_18    148         278F. MARTINEZ     GOES            Season_18_19    52          259D. ALVAREZ      AGUADA          Season_17_18    114         246M. HICKS        H. MACABI       Season_17_18    140         245在 x 軸上我想放置輔助點,在 y 軸上放置非輔助點。但我也想按賽季、球隊和球員進行過濾,所以當我選擇球隊的一名確定球員時,我可以看到他們的分數用一種顏色顯示,而其他分數顯示為灰色,或者例如,如果我想選擇兩個或更多我可以在他們之間進行比較(用不同的顏色),并且其他點是可見的,但是是灰色的。我還想比較兩個不同球隊的球員和過濾器的組合。我正在學習數據科學,使用圖書館 plotly express 我可以制作散點圖并按團隊過濾,我可以比較兩個不同的團隊(或賽季或球員)。但是我無法以一種奇特的方式添加多個過濾器,而且我也不知道如何顯示選定的過濾器并將其他過濾器置于灰色(不會使它們消失)。代碼如下:import plotly.express as pxfig = px.scatter(pointsperplayer, x='assisted', y='notassisted', hover_name='player',                  hover_data=['team_name','season'], color='season')fig.show()圖形結果如下:總而言之,我想要三個過濾器,一個是賽季,另一個是球隊,另一個是球員,能夠在每個過濾器中有多個選擇,并獲得不同的顏色,其余的點變灰,這樣我就可以將結果與其他結果進行比較,我不確定是否可以使用 plotly express 或者我是否應該使用不同的庫。
查看完整描述

1 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

所以我無法操縱圖例,但我可以通過在此處找到的下拉小部件添加過濾器。根據您的 IDE,您可能需要使用 Jupyter 來使小部件工作。我遇到了 VSCode 無法顯示小部件的問題。我下面的功能是按球隊名稱、賽季或球員進行過濾,并在該過濾器中比較兩個選項。我希望這可以擴展以滿足您的需求。


import pandas as pd

import plotly.express as px

import plotly.graph_objects as go

import ipywidgets as ipy

from ipywidgets import Output, VBox, widgets



# First gather the data I need and choose the display colors

playerData = pd.read_csv("playerData.csv")

teamNames = list(playerData['team_name'].unique().tolist());

seasons = list(playerData['season'].unique().tolist());

players = list(playerData['player'].unique().tolist());

color1 = 'red'

color2 = 'blue'

color3 = 'gray'


# This creates the initial figure.

# Note that px.scatter generates multiple scatter plot 'traces'. Each trace contains 

# the data points associated with 1 team/season/player depending on what the property

# of 'color' is set to.

trace1 = px.scatter(playerData, x='assisted', y='notassisted', color='team_name')

fig = go.FigureWidget(trace1)


# Create all our drop down widgets

filterDrop = widgets.Dropdown(

    description='Filter:',

    value='team_name',

    options=['team_name', 'season','player']  

)

teamDrop1 = widgets.Dropdown(

    description='Team Name:',

    value='NACIONAL',

    options=list(playerData['team_name'].unique().tolist())  

)

teamDrop2 = widgets.Dropdown(

    description='Team Name:',

    value='NACIONAL',

    options=list(playerData['team_name'].unique().tolist())  

)

playerDrop1 = widgets.Dropdown(

    description='Player:',

    value='A. DANRIDGE',

    options=list(playerData['player'].unique().tolist())  

)

playerDrop2 = widgets.Dropdown(

    description='Player:',

    value='A. DANRIDGE',

    options=list(playerData['player'].unique().tolist())  

)

seasonDrop1 = widgets.Dropdown(

    description='Season:',

    value='Season_17_18',

    options=list(playerData['season'].unique().tolist())  

)

seasonDrop2 = widgets.Dropdown(

    description='Season:',

    value='Season_17_18',

    options=list(playerData['season'].unique().tolist())  

)


# This will be called when the filter dropdown changes. 

def filterResponse(change):

    # generate the new traces that are filtered by teamname, season, or player

    tempTrace = px.scatter(playerData, x='assisted', y='notassisted', color=filterDrop.value)

    with fig.batch_update():

        # Delete the old traces and add the new traces in one at a time

        fig.data = []

        for tr in tempTrace.data:

            fig.add_scatter(x = tr.x, y = tr.y, hoverlabel = tr.hoverlabel, hovertemplate = tr.hovertemplate, \

                           legendgroup = tr.legendgroup, marker = tr.marker, mode = tr.mode, name = tr.name)

    # Call response so that it will color the markers appropriately

    response(change)


# This is called by all the other drop downs

def response(change):

    # colorList is a list of strings the length of the # of traces 

    if filterDrop.value == 'team_name':

        colorList = [color1 if x == teamDrop1.value else color2 if x == teamDrop2.value else color3 for x in teamNames]

    elif filterDrop.value == 'season':

        colorList = [color1 if x == seasonDrop1.value else color2 if x == seasonDrop2.value else color3 for x in seasons]

    else:

        colorList = [color1 if x == playerDrop1.value else color2 if x == playerDrop2.value else color3 for x in players]

    with fig.batch_update():

        # Color each trace according to our chosen comparison traces

        for i in range(len(colorList)):

            fig.data[i].marker.color = colorList[i]


# These determine what function should be called when a drop down changes

teamDrop1.observe(response, names="value")

seasonDrop1.observe(response, names="value")

playerDrop1.observe(response, names="value")

teamDrop2.observe(response, names="value")

seasonDrop2.observe(response, names="value")

playerDrop2.observe(response, names="value")

filterDrop.observe(filterResponse, names="value")


# HBox and VBox are used to organize the other widgets and figures

container1 = widgets.HBox([filterDrop]) 

container2 = widgets.HBox([teamDrop1, seasonDrop1, playerDrop1])

container3 = widgets.HBox([teamDrop2, seasonDrop2, playerDrop2])

widgets.VBox([container1, container2, container3, fig])


結果如下所示

查看完整回答
反對 回復 2022-06-14
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號