Created
June 5, 2020 11:39
-
-
Save dsibi/e042a5f8ef8fe6973585b7b7b636a4d4 to your computer and use it in GitHub Desktop.
Meteors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#I have issue with wrong reflection data in hovers in the code below. See code with comments for each block. | |
import plotly.express as px | |
import pandas as pd | |
import plotly.graph_objects as go | |
rows=[['501-600','15','122.58333','45.36667','Name1'], | |
['till 500','4','12.5','27.5','Name2'], | |
['more 601','41','-115.53333','38.08','Name3'], | |
['till 500', '26', '65.5', '29.5','Name4'], | |
['501-600','35','12.58333','55.36667','Name5'], | |
['more 601','9','55.53333','-38.08','Name6'], | |
] | |
colmns=['bins','data','longitude','latitude','names'] | |
#Df creation | |
df=pd.DataFrame(data=rows, columns=colmns) | |
#Ordering for labels in legend | |
order = ['till 500', '501-600', 'more 601'] | |
df = df.set_index('bins') | |
df_ordered = df.T[order].T.reset_index() | |
df_ordered = df_ordered.astype({"data": int}) | |
# | |
# #Plotting viz | |
# fig=px.scatter_geo(df_ordered,lon='longitude', lat='latitude', | |
# color='bins', | |
# color_discrete_sequence=px.colors.qualitative.Set1, | |
# hover_name="names", | |
# size='data',opacity=0.7,text='data', | |
# projection="equirectangular",size_max=35, | |
# ) | |
# | |
# #Adding custom data for hovers | |
# fig.update_traces(customdata=df_ordered) | |
# fig.update_traces(hovertemplate="<b>Name: %{customdata[4]} </b><br><br>Bin: %{customdata[0]}<br>" | |
# "Data: %{customdata[1]:.2f}<extra></extra>") | |
# | |
# #Adding marker labels | |
# fig.add_trace(go.Scattergeo(lon=df_ordered["longitude"], | |
# lat=df_ordered["latitude"], | |
# text=df_ordered["names"], | |
# textposition="middle left", | |
# mode='text', | |
# textfont=dict(size=12,color="black"), | |
# showlegend=False, | |
# texttemplate=" %{text}", | |
# hoverinfo='skip', | |
# )) | |
# fig.show() | |
# | |
# #So at the end I guess that this problems raise from ordering and maybe I need to remake smth in customdata lines, but can't understand how to fix it. WIll appreciate for help in fixing it. | |
fig = px.scatter_geo(df_ordered, | |
lon='longitude', | |
lat='latitude', | |
color='bins', | |
color_discrete_sequence=px.colors.qualitative.Set1, | |
hover_name="names", | |
size='data', | |
opacity=0.7, | |
text='names', | |
projection="equirectangular", | |
size_max=35, | |
# by default every column go to hover | |
# you can eventually use formatting here | |
hover_data={"longitude": False, | |
"latitude": False, | |
"names": False, | |
"data": ":.2f"} | |
) | |
# it's pretty verbose but now the output should be | |
# exactly as you expect | |
for data in fig.data: | |
template = data.hovertemplate | |
template = template.replace("<b>", "<b>Name: ")\ | |
.replace("bins=", "Bin: ")\ | |
.replace("data=", "Data: ") | |
data.hovertemplate = template | |
fig.add_trace(go.Scattergeo(lon=df_ordered["longitude"], | |
lat=df_ordered["latitude"], | |
text=df_ordered["names"], | |
textposition="middle left", | |
mode='text', | |
textfont=dict(size=12,color="black"), | |
showlegend=False, | |
texttemplate=" %{text}", | |
hoverinfo='skip', | |
)) | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment