Linked Questions

Popular Questions

Animate Plotly Graph Object

Asked by At

I am creating a choropleth graph, to illustrate the US election results by state. I am using plotly's graph_objects, as I am drawing two different parties results on one graph:

{'year': {3: 1976,
  4: 1976,
  8: 1976,
  14: 1976,
  17: 1976,
  21: 1976,
  24: 1976,
  25: 1976,
  28: 1976,
  31: 1976},
 'state_po': {3: 'AK',
  4: 'AL',
  8: 'AR',
  14: 'AZ',
  17: 'CA',
  21: 'CO',
  24: 'CT',
  25: 'DC',
  28: 'DE',
  31: 'FL'},
 'party_simplified': {3: 'REPUBLICAN',
  4: 'DEMOCRAT',
  8: 'DEMOCRAT',
  14: 'REPUBLICAN',
  17: 'REPUBLICAN',
  21: 'REPUBLICAN',
  24: 'REPUBLICAN',
  25: 'DEMOCRAT',
  28: 'DEMOCRAT',
  31: 'DEMOCRAT'},
 'candidatevotes': {3: 71555,
  4: 659170,
  8: 498604,
  14: 418642,
  17: 3882244,
  21: 584278,
  24: 719261,
  25: 137818,
  28: 122461,
  31: 1636000},
 'percentage': {3: 57.90457539611893,
  4: 55.727268884473936,
  8: 64.96172812966185,
  14: 56.36613577948053,
  17: 49.748313955946934,
  21: 54.02777777777777,
  24: 51.881444507359234,
  25: 81.63122667772316,
  28: 51.969088702353574,
  31: 51.92610623078361},
 'STATE': {3: 'AK',
  4: 'AL',
  8: 'AR',
  14: 'AZ',
  17: 'CA',
  21: 'CO',
  24: 'CT',
  25: 'DC',
  28: 'DE',
  31: 'FL'},
 'geometry': {3: <MULTIPOLYGON (((-179.108 51.301, -179.105 51.3, -179.101 51.302, -179.101 5...>,
  4: <MULTIPOLYGON (((-88.11 30.259, -88.106 30.258, -88.104 30.256, -88.102 30.2...>,
  8: <POLYGON ((-91.63 36.499, -91.628 36.499, -91.618 36.499, -91.61 36.499, -91...>,
  14: <POLYGON ((-110.491 37.004, -110.487 37.003, -110.478 37, -110.473 36.999, -...>,
  17: <MULTIPOLYGON (((-118.426 32.8, -118.426 32.8, -118.427 32.8, -118.427 32.8,...>,
  21: <POLYGON ((-106.876 41.003, -106.858 41.003, -106.848 41.003, -106.751 41.00...>,
  24: <MULTIPOLYGON (((-73.622 40.984, -73.622 40.984, -73.622 40.983, -73.624 40....>,
  25: <MULTIPOLYGON (((-77.031 38.808, -77.031 38.808, -77.031 38.809, -77.031 38....>,
  28: <MULTIPOLYGON (((-75.227 38.589, -75.227 38.589, -75.228 38.589, -75.228 38....>,
  31: <MULTIPOLYGON (((-81.963 24.521, -81.964 24.521, -81.964 24.521, -81.965 24....>},
 'X': {3: -152.24098487629138,
  4: -86.82675798860355,
  8: -92.43920164001888,
  14: -111.66457246910028,
  17: -119.60818137544308,
  21: -105.547827393017,
  24: -72.7262299867642,
  25: -77.01464422650051,
  28: -75.50592275367421,
  31: -82.50934005309693},
 'Y': {3: 64.24018581436499,
  4: 32.793537825805934,
  8: 34.89976858904505,
  14: 34.29323028581209,
  17: 37.24537297367808,
  21: 38.9985521135609,
  24: 41.62196289867711,
  25: 38.90931803427013,
  28: 38.9955937074045,
  31: 28.674017716842922}}

fig=make_subplots(specs=[[{'secondary_y':False}]])
trace1=go.Choropleth(name='Republicans',
                     locations=winners.query('party_simplified=="REPUBLICAN"')['state_po'],
                     z=winners.query('party_simplified=="REPUBLICAN"')['percentage'],
                    locationmode='USA-states',colorscale='Reds')
trace2=go.Choropleth(name='Democrats',
                     locations=winners.query('party_simplified=="DEMOCRAT"')['state_po'],
                     z=winners.query('party_simplified=="DEMOCRAT"')['percentage'],
                     locationmode='USA-states',colorscale='Blues')

fig.add_trace(trace1)
fig.add_trace(trace2)

fig.update_layout(title=dict(text='Winner Parties by State per Year',font_family='Arial Black',font_color='black'),
                  geo_scope='usa',animation_frame=winners['year'])
fig.show()

Winners dataframe contains information on elections from 1976 to 2020. I need to animate the graph using animaton_frame function, but it does not work here, unlike regular px.choropleth graph.

Any suggestions?

Related Questions