Newly Data Points Hidden Behind the Previously Plotted data -- plotly.express as px, and plotly.graph_objects as go

If I read the same data from external files, it does not work. Like if I plot the data of map coordniates (from Excel file) and then plot ambulance data (from Excel file) , ambulance symbol in the diamond shape appears behind the map. It is not shown on the front.

First Plotted Data: (A map generated using coordinate data) - Output

Now, I want plot and show ambulance locations on the above-generated map (Keep everything in the graph)

Plotted some ambulance locations (New output)

As we can see above, the new plotted data for ambulance location is hidden due to previous plotted data. How do we solve this?

Full Code:

import plotly.express as px
import plotly.graph_objects as go
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
from openpyxl import load_workbook
import numpy as np
import pandas as pd
from mplcursors import cursor

import numpy as np; 
np.random.seed(0)
import seaborn as sns;
from matplotlib.pyplot import figure
import pandas as pd
import numpy as np
import io

file_loc_1 = "AgeGroupData_time_to_treatment.xlsx"
df_centroid_Coord = pd.read_excel(file_loc_1, index_col=None, na_values=['NA'])
df_centroid_Coord

file_loc2Amb = "Ambulance IDs.xlsx"
df_station = pd.read_excel(file_loc2Amb, index_col=None, na_values=['NA'], usecols="A:D")

df_station.rename(columns={'Longtitude':'x', 'Latitude':'y'}, inplace=True)
df_centroid_Coord['Ambulance_Treatment_Time'] = df_centroid_Coord ['Base_TT']

fig = px.scatter(df_centroid_Coord, x="x", y="y", 
                 title="Southern Region Centroids", 
                 color='Ambulance_Treatment_Time', 
                 log_x=True,
                 size_max=60, 
                 color_continuous_scale='Reds',
                 range_color=(0.5,2),
                 )

fig.update_traces(marker={'size': 4, 'symbol': 1})

fig.add_trace(go.Scatter(x=df_station['x'],
                         y=df_station['y'],
                         mode='markers+text',
                         text=df_station['Ambulance station name'],
                         textposition='top center',
                         showlegend=True,
                         marker=dict(
                             size=12,
                             symbol=2,
                             color='black'
                         )
                        )
             )

fig.update_layout(width=1000, height=800, paper_bgcolor="LightSteelBlue")

fig.show()
1 Like

Hi @EmAdilAbid, welcome to the forum.

I can’t reproduce this behaviour.

I’m not sure, but try using plotly.graph_objects for the first scatter plot instead of plotly.express.

Here the lines of your code I am refering to.