Home > Back-end >  Dash Choropleth Map Color based on frequency or count
Dash Choropleth Map Color based on frequency or count

Time:10-28

Let's say I have a dataset of scientific publications, their Country, and a score metric that evaluates the quality of the publications. Sort of like the following:

Paper Country Score
Pub 1 USA 5
Pub 2 China 7
Pub 3 Japan 9
Pub 4 China 4

I want to generate a map that is colored based on total score per country. For example, China would have the highest color score of 11, Japan next with 9, and USA last with 5. Additionally, I would like to generate another map that is colored based on total paper counts per country. In this case, China would have the highest color with a count of 2 papers, and Japan/USA would be tied with a count of 1 paper.

The code to use is as follows:

fig = px.choropleth(df, locations = df['Country'], color=???)

My problem is that it seems that the color argument requires me to pass a column from my source data without performing any aggregation functions (ie. sum/count).

Rather than base my color on a certain column cell value, I would like to base it on an aggregation of the column data. I know a workaround is to create a brand new dataframe that has the data aggregated, and then pass that, but I am wondering if this can be done natively in Dash without having to create a new dataframe per aggregation. I wasn't able to find any examples of this behavior in the Dash documentation. Any help is much appreciated!

CodePudding user response:

How about something like

fig = px.choropleth(df[['Country', 'Score']].groupby('Country').sum().reset_index(), 
                    locations = 'Country',
                    color='Score')

See example from Plotly here where you only need pass the column names.

  • Related