Choropleth: different opacity for each region


I’ve been trying to display average salaries on a choropleth object, using color to communicate the ammount, as shown below:

However, I have a different ammount of entries for each country. That makes some strange artifacts in countries like Israel with only one entry with a very high pay. Instead of removing these, I would like instead to use transparency to convey “confidence” in the data: countries with low data will be less opaque. I already have a dictionary that correponds each country to its transparency. However, I can’t find a way to apply these values to the map. Here’s what I’ve tried:

opacities_dict = {k: np.log(len(df[df[select_filter] == k])) for i, k in enumerate(x) if k not in x[:i]}
opacities_dict = {k: (v + 1) / (max(opacities_dict.values()) + 1) for k, v in opacities_dict.items()}

#opacities_dict is something like: {"AU":0.7845698, "PT": 0.5698655, "US":1.0, ...}

fig = go.Figure(data=go.Choropleth(
        locations = salaries['ISO_A2'],
        text = salaries['ISO_A2'],
        z = salaries['salary'],
        colorbar_title="Salary in US$",
            # opacity=list(opacities_dict.values()), #this doesn't work, is overriden and opacity is set to 1
            opacity=0.4, #this sets the whole map to the same opacity
            # opacity = [0.4] #this is accepted but sets the whole map to the same opacity

I can’t find any documentation on passing an array of opacities to the opacity attribute. The only thing I find in the docs is that a 1-d array should be accepted, although I can’t see what the point would be if I could only pass a 1-value 1-d array:

(Choropleth traces in Python)

Does anyone know how to achieve this, or something similar?

1-d array is equivalent to a list, it doesn’t mean ‘of length 1’

I see in your commented example that you wrote lens_dicts rather than opacities_dicts, is it a typo on your message? If you passed a list with all numbers greater than 1 I wouldn’t be surprised it just truncated it to anyway.

Hi there. Yes, that was a typo. In my original code I called that variable lens_dict instead of opacities_dict. I renamed it here for clarity but it went unnoticed there. Just edited. Thanks!

Now, about the 1-d array, yes, it does acccept a list. No exceptions thrown, as stated in the docs. However, no matter the size (len()) of the list I pass, it just assumes one opacity for every country. So, if I pass a list with length 1, like [0.4], it assumes that one value as the general opacity. If I pass a longer list (say with the length of the number of countries in the data) , like [0.3, 0.7, 0.4, 0.2, …, 0.6, 0.9], the opacity is set to 1 in the whole map, no difference between countries whatsoever.

Tried as well, looks like a bug. You might want to raise an issue on github.