raceplotly icon indicating copy to clipboard operation
raceplotly copied to clipboard

error when trying to use custom colors

Open titopuertolara opened this issue 1 year ago • 5 comments

Hi: i'm trying to customize color in my plot but i'm having this error

` /.local/lib/python3.10/site-packages/raceplotly/plots.py in __get_colors(self) 296 elif (self.item_color != None and len(self.item_color) != self.df[self.item_column].nunique()): 297 for item in self.df[self.item_column].unique(): --> 298 if item not in self.item_color.keys(): 299 self.item_color[item] = 'rgb({}, {}, {})'.format(*sample(range(256), 3)) 300 self.df['color'] = self.df[self.item_column].map(self.item_color)

AttributeError: 'str' object has no attribute 'keys' `

in that case i tried tu run your example with the same outcome

titopuertolara avatar May 03 '23 23:05 titopuertolara

Hi @titopuertolara, thank you for raising this issue.

To better understand the issue, could you please provide more information on how you're trying to give custom colors? It would be helpful if you could share the code snippet or example that you're working with.

siddarth-patil avatar May 04 '23 10:05 siddarth-patil

So I had a similar issue and it seems that I was setting too many colors for the item_color argument. When I commented some out it would work. I was wondering if there was a way to increase max arguments for the item_color.

kevindangkhoama avatar Apr 09 '24 16:04 kevindangkhoama

Hi @kevindangkhoama thank you for reaching out.

Can you please share the code snippet that you were using before you encountered the error? Also, could you share the screenshot of the error message that you received?

siddarth-patil avatar Apr 09 '24 19:04 siddarth-patil

Hello @siddarth-patil. If I comment out one of the player in colors it works as intended and the one player not assigned is still shown during the animation.

from raceplotly.plots import barplot

app = dash.Dash(__name__)

app.layout = html.Div([
    html.H1("Fantasy Points Race"),
    dcc.Dropdown(
        id='stat-dropdown',
        options=[
            {'label': 'Fantasy Points', 'value': 'Fantasy Points'},
            {'label': 'Points (PTS)', 'value': 'PTS'},
            {'label': 'Assists (AST)', 'value': 'AST'},
            {'label': 'Rebounds (REB)', 'value': 'REB'},
            {'label': 'Blocks (BLK)', 'value': 'BLK'},
            {'label': 'Steals (STL)', 'value': 'STL'},
            {'label': '3-Pointers Made (3PTM)', 'value': '3PTM'},
            {'label': 'Field Goals Made (FGM)', 'value': 'FGM'},
            {'label': 'Free Throws Made (FTM)', 'value': 'FTM'}
        ],
        value='Fantasy Points'  
    ),
    dcc.Graph(id='race-plot')
])

@app.callback(
    Output('race-plot', 'figure'),
    [Input('stat-dropdown', 'value')]
)
def update_race_plot(selected_stat):
    cumulative_df = cleaned_df.copy()
    cumulative_df['Week'] = cumulative_df['Week'].astype(int)
    cumulative_df = cumulative_df.groupby(['Player', 'Week'])[selected_stat].sum().groupby('Player').cumsum().reset_index()

    players = cumulative_df['Player'].unique()
    week_0_df = pd.DataFrame({'Player': players, 'Week': 0, selected_stat: 0})
    
    cumulative_df = pd.concat([week_0_df, cumulative_df])

    colors = {
    "Aaron Gordon": "rgb(8,48,107)",
    "Ausar Thompson": "rgb(8,81,156)",
    "Austin Reaves": "rgb(33,113,181)",
    "Chris Paul": "rgb(66,146,198)",
    "Coby White": "rgb(107,174,214)",
    "Damian Lillard": "rgb(158,202,225)",
    "Darius Garland": "rgb(198,219,239)",
    "Dejounte Murray": "rgb(222,235,247)",
    "Deni Avdija": "rgb(247,251,255)",
    "Dereck Lively II": "rgb(8,48,107)",
    "Devin Booker": "rgb(8,81,156)",
    "Donte DiVincenzo": "rgb(33,113,181)",
    "Franz Wagner": "rgb(66,146,198)",
    "Grayson Allen": "rgb(107,174,214)",
    "Jalen Duren": "rgb(158,202,225)",
    "Jalen Johnson": "rgb(198,219,239)",
    "Jamal Murray": "rgb(222,235,247)",
    "Jarrett Allen": "rgb(247,251,255)",
    "Jonas Valanciunas": "rgb(8,48,107)",
    "Josh Okogie": "rgb(8,81,156)",
    "Kawhi Leonard": "rgb(33,113,181)",
    "Keldon Johnson": "rgb(66,146,198)",
    "Khris Middleton": "rgb(107,174,214)",
    "Lonnie Walker IV": "rgb(158,202,225)",
    "Marvin Bagley III": "rgb(198,219,239)",
    "Michael Porter Jr.": "rgb(222,235,247)",
    "Mike Conley": "rgb(247,251,255)",
    "Onyeka Okongwu": "rgb(8,48,107)",
    "Tyrese Haliburton": "rgb(8,81,156)",
    "Vince Williams Jr.": "rgb(33,113,181)",
    "Walker Kessler": "rgb(66,146,198)",
    "Wendell Carter Jr.": "rgb(107,174,214)",
    "Xavier Tillman": "rgb(158,202,225)"
}


    cumulative_df['color'] = cumulative_df['Player'].map(colors)

    my_raceplot = barplot(cumulative_df,
                          item_column='Player',
                          value_column=selected_stat,
                          top_entries=10,
                          item_color=colors,
                          time_column='Week')

    fig = my_raceplot.plot(title=f'Bar Chart Race by Top 10 {selected_stat}',
                           item_label='Players',
                           value_label=selected_stat,
                           time_label='Week: ',
                           frame_duration=800)  
    
    fig.update_layout(
        height = 500,
    )
    
    return fig


if __name__ == '__main__':
    app.run_server(jupyter_mode='tab', debug=True)

Screenshot 2024-04-09 at 7 16 53 PM

kevindangkhoama avatar Apr 09 '24 23:04 kevindangkhoama

I have the same problem where I have 7 unique values for the item_column and have set 7 unique colours for each of the items. This throws TypeError: unhashable type: 'dict' caused by the following line in the __get_colors(self) method: self.df=self.df.rename(columns={self.item_color: "color"})

This seems to occur only when the number of unique values in the item_column is equal to the number of unique colours. I propose changing the line above to: self.df['color'] = self.df[self.item_column].map(self.item_color)

So far, this has seemed to work for me. @lucharo or @siddarth-patil, could you please confirm?

rraj786 avatar Aug 11 '24 06:08 rraj786