Hello,
Iāve recently started working with Python and Iām really excited to have found Plotly.
Iām working on a small project in which Iām collecting weather forecast data and Iād like to plot each days forecast on a map. To begin, my data sits in a csv file with the following columns:
AirportCode, Location2, Lat, Lon, validDate, AVG, ANOM
KNYC,New York City, xx.xxx, yy.yyy, 2018-08-01, 72, -2
KNYC,New York City, xx.xxx, yy.yyy, 2018-08-02, 74, 1
KNYC,New York City, xx.xxx, yy.yyy, 2018-08-03, 78, 3
ā¦
KPDX, Portland, xx.xxx, yy.yyy, 2018-08-01, 85,2
KPDX, Portland, xx.xxx, yy.yyy, 2018-08-02, 92,-2
KPDX, Portland, xx.xxx, yy.yyy, 2018-08-03, 87, -1
where Lat = Latitude, Lon = longitude, validDate = the forecast date (10 days for each city), AVG = average forecasted temperature and ANOM = deviation from normal.
So far I have been able to produce maps using plotly.express and plotly.graph_objects. Both seem to get me 90% percent of the way but in different ways.
For example with plotly.express, using the following code I get a map with a slider/animation (yes!) using the AVG field however what I would really like to see is the deviation from normal (ANOM) but I get the error that the size property contains invalid elements (all the negative anomalyās).
import plotly.express as px
import pandas as pd
df = pd.read_csv(rā/Users/myname/Downloads/Plotly/CITY_IMPORT_MAP.csvā)
fig = px.scatter_geo(df,
lat = āLatā,
lon = āLonā,
color = āANOMā,
#color_continuous_scale = ārdbuā,
color_continuous_scale=px.colors.diverging.RdBu[::-1],
hover_name= āLocation2ā,
size = āANOMā,
size_max = 10,
animation_frame = āvalidDateā,
title = āTemperature Anomalyāsā,
projection = āalbers usaā)
fig.show()
When I use plotly.graph_objects I can get a map with all the cities plotted using the ANOM field (yes!) however I donāt know how to add a slider to animate/scroll through each of the forecast dates (validDate). My code for this looks like the following
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv(rā/Users/myname/Downloads/Plotly/CITY_IMPORT_MAP.csvā)
df[ātextā] = df[āLocation2ā] + ā ā + df[āANOMā].astype(str)
fig = go.Figure(data=go.Scattergeo(
locationmode = āUSA-statesā,
lon = df[āLonā],
lat = df[āLatā],
text = df[ātextā],
mode = āmarkersā,
marker = dict(
size = 10,
opacity = 0.8,
reversescale = True,
autocolorscale = False,
symbol = ācircleā,
line = dict(
width=1,
color=ārgba(102, 102, 102)ā
),
colorscale = āRdBuā,
cmin = -13,
color = df[āANOMā],
cmax = 13,
colorbar_title=āTemp Anomā
)))
fig.update_layout(
title = āL48 Temp Anom
(Hover for airport names)ā,
geo = dict(
scope=āusaā,
projection_type=āalbers usaā,
showland = True,
landcolor = ārgb(250, 250, 250)ā,
subunitcolor = ārgb(217, 217, 217)ā,
countrycolor = ārgb(217, 217, 217)ā,
countrywidth = 0.5,
subunitwidth = 0.5
),
)
fig.show()
Is there anyway to use anomalyās using express or to animate my plot that uses graph_objects?
I donāt want to get ahead of myself but if I can one of these options to work then the next item to tackle is to either export to a gif or make my plot publicly accessible.
Thanks for reading!