Reputation: 33
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?
Upvotes: 1
Views: 1917
Reputation: 35115
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()
Upvotes: 2