简体   繁体   中英

How to name and color different traces plotly graph objects?

I have following problem using plotly graph objects: I am currently working with airline-data. My aim is to create a bubble / scatter plot where I can show which airline, traveled how far and how many flights they needed.

The problem is, that I can't get the points to match the legend correctly.

df = pd.read_csv(PATH, sep=',')
df_grouped_Distance_Sum = df.groupby('AIRLINE')["DISTANCE"].sum()
print(df_grouped_Distance_Sum)

df_grouped_Flights = df.groupby('AIRLINE')["counter"].sum()
print(df_grouped_Flights)


hover_text = []

for index, row in df.iterrows():
    hover_text.append(('Airline: {AIRLINE}').format(AIRLINE=row['AIRLINE']))

df['hover_text'] = hover_text
# creating a dict to map the airline-names
airline_names = ['AA - American Airlines', 'AS - Alaska Airlines', 'B6 - JetBlue Airways', 'DL - Delta Air Lines Inc.', 'EV - Atlantic Southeast Airlines', 'F9 - Frontier Airlines Inc.',
                 'HA - Hawaiian Airlines Inc.', 'MQ - American Eagle Airlines Inc.', 'NK - Spirit Air Lines', 'OO - Skywest Airlines Inc.', 'UA - United Air Lines Inc.', 'US - US Airways Inc.', 'VX - Virgin America', 'WN - Southwest Airlines Co.']

airline_data = {airline: df.query("AIRLINE == '%s'" % airline)
                for airline in airline_names}

fig = go.Figure()

for airline_name, airline in airline_data.items():
    fig.add_trace(go.Scatter(y=df_grouped_Flights,
                             x=df_grouped_Distance_Sum, name=airline_name))

fig.update_traces(mode='markers')


fig.show()

As seen in the picture all points on the graph are named and colored as the last Airline

For example the bottom left point should be HA - Hawaiian Airlines and pink not purple..

So how can I change it, that all Points match the correct airlines?

The data to create the graph was created appropriately, so please replace it. Specify the airline name in the loop process of the scatter plot.

import plotly.graph_objects as go
import pandas as pd
import numpy as np
import random

airline_names = ['AA - American Airlines', 'AS - Alaska Airlines', 'B6 - JetBlue Airways', 'DL - Delta Air Lines Inc.', 'EV - Atlantic Southeast Airlines', 'F9 - Frontier Airlines Inc.',
                 'HA - Hawaiian Airlines Inc.', 'MQ - American Eagle Airlines Inc.', 'NK - Spirit Air Lines', 'OO - Skywest Airlines Inc.', 'UA - United Air Lines Inc.', 'US - US Airways Inc.', 'VX - Virgin America', 'WN - Southwest Airlines Co.']
df = pd.DataFrame({'AIRLINE':random.choices(airline_names, k=200),'DISTANCE':np.random.randint(500,10000,(200,))})

dfs = df.groupby('AIRLINE').agg(['count','sum'])
dfs.reset_index(inplace=True)
dfs.columns = ['AIRLINE','COUNT','DISTANCE']

fig = go.Figure()

for idx, row in dfs.iterrows():
    fig.add_trace(go.Scatter(x=[row['DISTANCE']], y=[row['COUNT']], name=row['AIRLINE']))

fig.update_traces(mode='markers')

fig.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