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!