0

I am working on composing maps with Plotly in Jupyter Notebook and most are working, but on some, it is not displaying properly - see photo.

I cannot seem to find out why it is not displaying or how to resolve it.

image of Plotly map without a scale on the right side

#DESTINATION Delay for each airport within a carrier network plotted on map
airline_data_frames = []
airport_list = df['DEST_AIRPORT_ID'].tolist()
airport_set = set(airport_list)
for carrier in carrier_dict:
    cur_carrier_df = carrier_dict[carrier]
    dict_airport=get_dataframe_unique_column_DEST_AIRPORT_ID(cur_carrier_df)
    out_df = pd.DataFrame(columns=["NAME", "ARR_DELAY_MEAN","PERCENT_OF_AIR_TRAFFIC",         
    "LONGITUDE","LATITUDE"])
    for name in airport_set:
        if name not in (dict_airport):
            continue
        cur_df = dict_airport[name]
        airport = cur_df['DEST_AIRPORT_ID'].iloc[0]
        dest = cur_df['DEST'].iloc[0]
        arr_delay = cur_df.ARR_DELAY.mean()
        if arr_delay < 0 :
            arr_delay=0
        length = len(cur_df)/len(cur_carrier_df) * 100
        lat, long = look_up_long_lat(dest)
        airport = cur_df['DEST_CITY_NAME'].iloc[0]
        out_df = out_df.append({"NAME": airport, "ARR_DELAY_MEAN": arr_delay,         
        "PERCENT_OF_AIR_TRAFFIC":length, 
        "LONGITUDE":long.values[0],"LATITUDE":lat.values[0]}, ignore_index=True)
    print(look_up_carrier_by_initial(carrier))
    print(out_df.describe())
    fig = px.scatter_geo(out_df, lon = 'LONGITUDE',lat = 'LATITUDE', color="ARR_DELAY_MEAN", 
    hover_name="NAME", size="PERCENT_OF_AIR_TRAFFIC",projection="albers usa",scope="USA")
    fig.show()
    fig = None
Jarred Parrett
  • 101
  • 1
  • 5

1 Answers1

0

In revisiting this, I found this within the Plotly documentation:

"Most Plotly Express functions accept a color argument which automatically assigns data values to discrete colors if the data is non-numeric. If the data is numeric, the color will automatically be considered continuous. This means that numeric strings must be parsed to be used for continuous color, and conversely, numbers used as category codes must be converted to strings."link to source

I resolved it by explicitly stating that the ARR_DELAY_MEAN column was of type float when I appended a line. However, this could also be done by changing a column to numeric with the pd.to_numeric function.

My Solution:

 out_df = out_df.append({"NAME": airport, "ARR_DELAY_MEAN": float(arr_delay),         
        "PERCENT_OF_AIR_TRAFFIC":length, 
        "LONGITUDE":long.values[0],"LATITUDE":lat.values[0]}, 
         ignore_index=True)

Other Possible Solution:

out_df["ARR_DELAY_MEAN"] = pd.to_numeric(out_df["ARR_DELAY_MEAN"])
Jarred Parrett
  • 101
  • 1
  • 5