简体   繁体   中英

Make Certain Points in Plotly Express Scatterplot different Colors

Currently, I have a scatterplot with each point representing a director, based on the production budget and profit they have. I want to highlight some of these points that standout with their own unique color, and create a legend saying who each one of those points are. For example, I want to highlight Joe Russo by making his dot on the graph Green, and then add him to a legend.

Right now, I have the scatterplot that is good. This is my current code:

BudProfDirector = px.scatter(BudProfDirectorDF, x = 'production_budget', y = 'Worldwide_Profit', hover_name='primary_name')

That gives this graph: Plotly Scatterplot

I also have a list of directors I want to highlight:

DirectorsToHighlight = ['Pierre Coffin', 'Chris Renaud', 'Kyle Balda','Zack Snyder', 'Christopher Nolan', 'Joe Russo']

But have no clue where to go from here

To color a specific point and reflect it in the legend, in this case it is easiest to add a graph by creating a new data frame of the same point size at the specific point. The graph is created based on the added data.

import plotly.express as px

DirectorsToHighlight = ['Pierre Coffin', 'Chris Renaud', 'Kyle Balda', 'Christopher Nolan', 'Joe Russo']

BudProfDirector = px.scatter(BudProfDirectorDF,
                             x='production_budget',
                             y='Worldwide_Profit',
                             hover_name='primary_name')
df = BudProfDirectorDF[BudProfDirectorDF['primary_name'].isin(DirectorsToHighlight)]

for row in df.itertuples():
    print(row)
    BudProfDirector.add_scatter(x=[row.production_budget],
                                y=[row.Worldwide_Profit],
                                hovertext=[row.primary_name],
                                hovertemplate='<b>%{hovertext}</b><br><br>production_budget=%{x}<br>Worldwide_Profit=%{y}<extra></extra>',
                                name=row.primary_name,
                                mode='markers')

BudProfDirector.show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM