0

I am plotting a map that shows the number of tweets by country. The countries with high number of tweets are supposed to be in dark color, while the low in light color. I am scaling the data based max_count of tweets value through conditional log scale (code below) in order to have a good distinction between color shades. However, I am not getting the result I want (countries with highly different values having same color shades). Any suggestions of how to do a good conditional log scaling?

if max_count > 10000000:
    max_scale = math.log(max_count, 1.0001)
elif max_count > 1000000:
    max_scale = math.log(max_count, 1.001)
elif max_count > 100000:
    max_scale = math.log(max_count, 1.005)
elif max_count > 50000:
    max_scale = math.log(max_count, 1.01)
elif max_count > 10000:
    max_scale = math.log(max_count, 1.1)
elif max_count > 1000:
    max_scale = math.log(max_count, 1.5)
else:
    max_scale = math.log(max_count, 1.7)

I should be getting result like this: map image where you can see clear color distinction between big and lower values.

Youcef
  • 1,103
  • 2
  • 11
  • 26
  • Did you try getting rid of the if/elif/else entirely and simply writing `max_scale = math.log10(max_count)`? – Stef Oct 20 '21 at 16:03
  • For instance, `[math.log10(x) for x in [1, 1000, 10000, 50000, 100000, 1000000, 10000000]] == [0.0, 3.0, 4.0, 4.7, 5.0, 6.0, 7.0]` – Stef Oct 20 '21 at 16:06
  • Maybe in combination with [matplotlib.colors.LinearSegmentedColormap](https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.LinearSegmentedColormap.html#matplotlib.colors.LinearSegmentedColormap.from_list) – Stef Oct 20 '21 at 16:08
  • Related questions: [A logarithmic colorbar in matplotlib scatter plot](https://stackoverflow.com/questions/17201172/a-logarithmic-colorbar-in-matplotlib-scatter-plot), [Python Logarithmic Colormap/palette](https://stackoverflow.com/questions/56457672/python-logarithmic-colormap-palette), [Logarithmic colorbar?](https://stackoverflow.com/questions/57246146/logarithmic-colorbar), – Stef Oct 20 '21 at 16:11
  • Related questions: [Logarithmic colormap in matplotlib](https://stackoverflow.com/questions/26253947/logarithmic-colormap-in-matplotlib), [Matplotlib, Log color scale on plot but linear colour scale on colorbar legend](https://stackoverflow.com/questions/26381314/matplotlib-log-color-scale-on-plot-but-linear-colour-scale-on-colorbar-legend) – Stef Oct 20 '21 at 16:12
  • See also [Matplotlib tutorial: Creating Colormaps in Matplotlib](https://matplotlib.org/stable/tutorials/colors/colormap-manipulation.html) – Stef Oct 20 '21 at 16:13
  • Thanks Stef for the suggestion. When doing max_scale =math.log10(max_count). I get worse results... ex: countries with count=915 ( which is max_count) and countries with count= 6 are both in same color darkness, which is not what I should get – Youcef Oct 20 '21 at 16:46
  • I should be getting result like this: https://twitter.com/landgeist/status/1448990036548984851/photo/1 where you can see clear color distinction between big and lower values – Youcef Oct 20 '21 at 16:47
  • I don't know what to tell you. `log10(915) == 2.0`, whereas `log10(6) == 0.8`. But those are numbers, not colours. How are you choosing the colours? – Stef Oct 20 '21 at 17:50
  • I strongly, strongly suggest you read the links I provided, especially the ones about colormaps. In any case, it's impossible to help you more than that, because I have no idea what you're asking. I can keep making random suggestions and you can keep saying "no, this doesn't work" without any other explanation, and you're getting nowhere in the process. I suggest reading [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and then rewriting your question so that it becomes possible to give you a meaningful answer. – Stef Oct 20 '21 at 17:55

0 Answers0