Plotly express colorscale map by absolute value instead of 0-1
Question:
I have a map of the US and am plotting winning margin in Presidential elections in Plotly Express. I want a winning margin of 0 to be displayed as white and the scale to diverge into red/blue. However, the color_continuous_scale keyword argument takes a range from 0-1, and where a winning margin of 0 falls in this 0-1 range varies each election.
Is there a workaround for this that would allow me to define 0 as white, and the extremes as red/blue?
i have tried to convert the winning margin of 0 to a fraction between 0 and 1 but this changes with every election cycle.
fig = px.choropleth(merged,
locationmode='USA-states',
locations='state_po',
animation_frame='year',
color='R_margin_x',
color_continuous_scale=[(0, 'blue'),
(merged['R_margin_x'].min()/(merged['R_margin_x'].min()-merged['R_margin_x'].max()), 'white'),
(1, 'red')],
scope='usa')
fig.show()
dataframe is of the form
party_simplified state_po year DEMOCRAT LIBERTARIAN OTHER REPUBLICAN D_pct_x L_pct_x O_pct_x R_pct_x R_margin_x D_pct_y L_pct_y O_pct_y R_pct_y R_margin_y R_lean
0 AK 2008 123594.0 1589.0 7173.0 193841.0 37.889374 0.487129 2.198978 59.424520 21.535146 52.761558 0.388418 1.488455 45.361569 -7.39999 28.935135
1 AL 2008 813479.0 NaN 19794.0 1266546.0 38.740434 NaN 0.942653 60.316913 21.576479 52.761558 0.388418 1.488455 45.361569 -7.39999 28.976468
2 AR 2008 422310.0 4776.0 21514.0 638017.0 38.864660 0.439529 1.979906 58.715904 19.851245 52.761558 0.388418 1.488455 45.361569 -7.39999 27.251234
3 AZ 2008 1034707.0 12555.0 16102.0 1230111.0 45.115251 0.547423 0.702079 53.635248 8.519997 52.761558 0.388418 1.488455 45.361569 -7.39999 15.919987
4 CA 2008 8274473.0 67582.0 208064.0 5011781.0 61.012638 0.498323 1.534180 36.954859 -24.057780 52.761558 0.388418 1.488455 45.361569 -7.39999 -16.657790
Answers:
In plotly you can pass the midpoint for continuous scales:
color_continuous_scale=px.colors.sequential.RdBu,
color_continuous_midpoint=0.0
The RdBu scale goes from red to white to blue. By passing color_continuous_midpoint=0.0
you specify that 0.0 is in the middle of the scale, i.e. white. The other colors will be determined accordingly. The same goes if you use a custom continuous color scale.
I have a map of the US and am plotting winning margin in Presidential elections in Plotly Express. I want a winning margin of 0 to be displayed as white and the scale to diverge into red/blue. However, the color_continuous_scale keyword argument takes a range from 0-1, and where a winning margin of 0 falls in this 0-1 range varies each election.
Is there a workaround for this that would allow me to define 0 as white, and the extremes as red/blue?
i have tried to convert the winning margin of 0 to a fraction between 0 and 1 but this changes with every election cycle.
fig = px.choropleth(merged,
locationmode='USA-states',
locations='state_po',
animation_frame='year',
color='R_margin_x',
color_continuous_scale=[(0, 'blue'),
(merged['R_margin_x'].min()/(merged['R_margin_x'].min()-merged['R_margin_x'].max()), 'white'),
(1, 'red')],
scope='usa')
fig.show()
dataframe is of the form
party_simplified state_po year DEMOCRAT LIBERTARIAN OTHER REPUBLICAN D_pct_x L_pct_x O_pct_x R_pct_x R_margin_x D_pct_y L_pct_y O_pct_y R_pct_y R_margin_y R_lean
0 AK 2008 123594.0 1589.0 7173.0 193841.0 37.889374 0.487129 2.198978 59.424520 21.535146 52.761558 0.388418 1.488455 45.361569 -7.39999 28.935135
1 AL 2008 813479.0 NaN 19794.0 1266546.0 38.740434 NaN 0.942653 60.316913 21.576479 52.761558 0.388418 1.488455 45.361569 -7.39999 28.976468
2 AR 2008 422310.0 4776.0 21514.0 638017.0 38.864660 0.439529 1.979906 58.715904 19.851245 52.761558 0.388418 1.488455 45.361569 -7.39999 27.251234
3 AZ 2008 1034707.0 12555.0 16102.0 1230111.0 45.115251 0.547423 0.702079 53.635248 8.519997 52.761558 0.388418 1.488455 45.361569 -7.39999 15.919987
4 CA 2008 8274473.0 67582.0 208064.0 5011781.0 61.012638 0.498323 1.534180 36.954859 -24.057780 52.761558 0.388418 1.488455 45.361569 -7.39999 -16.657790
In plotly you can pass the midpoint for continuous scales:
color_continuous_scale=px.colors.sequential.RdBu,
color_continuous_midpoint=0.0
The RdBu scale goes from red to white to blue. By passing color_continuous_midpoint=0.0
you specify that 0.0 is in the middle of the scale, i.e. white. The other colors will be determined accordingly. The same goes if you use a custom continuous color scale.