4
$\begingroup$

I have a custom Blend for use with GeoRegionValuePlot's ColorFunction option.

colfoo = Blend[{{-1, Red}, {-0.1, Lighter@Pink}, {0, Yellow}, 
     {0.1, Lighter@Green}, {10, Darker@Green}}, #] &

The items to plot have two countries with value 0; India and Russia.

vals = {Entity["Country", "UnitedStates"] -> -0.5, 
        Entity["Country", "India"] -> 0, 
        Entity["Country", "Canada"] -> 0.5, 
        Entity["Country", "Australia"] -> 1.75, 
        Entity["Country", "Russia"] -> 0, 
        Entity["Country", "Mexico"] -> 4.25};

I am expecting these to plot Yellow but they do not.

GeoRegionValuePlot[vals,
 GeoRange -> {{-60, 75}, {-130, 165}},
 ColorFunctionScaling -> False,
 ColorFunction -> colfoo,
 GeoBackground -> None]

enter image description here

Why aren't they plotting Yellow?

$\endgroup$
3
  • 3
    $\begingroup$ Definitely a bug, you can boil it down to a 2-country example: GeoRegionValuePlot[{Entity["Country", "UnitedStates"] -> -1, Entity["Country", "India"] -> 0}, GeoRange -> {{-60, 75}, {-130, 165}}, ColorFunctionScaling -> False, ColorFunction -> (Blend[{{-1, Red}, {0, Yellow}}, #] &), GeoBackground -> None] and they both come out red $\endgroup$
    – Jason B.
    Jul 20 '16 at 20:15
  • $\begingroup$ @JasonB I've reported it to WRI. Let's see what they say. I suppose I will have to plot it directly with GeoGraphics if it turns out it is a bug. $\endgroup$
    – Edmund
    Jul 20 '16 at 23:07
  • $\begingroup$ thanks for reporting it, I think this applies any time you have a small number of points and a custom color function. I can think of a workaround that still uses GeoRegionValuePlot, will post tomorrow. $\endgroup$
    – Jason B.
    Jul 20 '16 at 23:33
1
$\begingroup$

That's a funky bug. As JasonB points out in the comments, this can be reduced to a two-country example:

GeoRegionValuePlot[
 {
  Entity["Country", "UnitedStates"] -> -1,
  Entity["Country", "India"] -> 0
  }
 , GeoRange -> {{-60, 75}, {-130, 165}}
 , ColorFunctionScaling -> False
 , ColorFunction -> (Blend[{{-1, Red}, {0, Yellow}}, #] &)
 , GeoBackground -> Automatic
 , ImageSize -> 800
 ]

Mathematica graphics

This appears to be caused by the presence of negative values. Shifting the range in the above example up from {-1, 0} to {0, 1} produces

GeoRegionValuePlot[
 {
  Entity["Country", "UnitedStates"] -> 0,
  Entity["Country", "India"] -> 1
  }
 , GeoRange -> {{-60, 75}, {-130, 165}}
 , ColorFunctionScaling -> False
 , ColorFunction -> (Blend[{{0, Red}, {1, Yellow}}, #] &)
 , GeoBackground -> Automatic
 , ImageSize -> 800
 ]

Mathematica graphics

Similarly, for your example, if you start with

vals = {
   Entity["Country", "UnitedStates"] -> -0.5
   , Entity["Country", "India"] -> 0
   , Entity["Country", "Canada"] -> 0.5
   , Entity["Country", "Australia"] -> 1.75
   , Entity["Country", "Russia"] -> 0
   , Entity["Country", "Mexico"] -> 4.25
   };

colfoo = Function[
  Blend[{
    {-1, Red},
    {-0.1, Lighter@Pink},
    {0, Yellow},
    {0.1, Lighter@Green},
    {10, Darker@Green}
    }, #]
  ]

and you shift them up using

vals /. {Rule[entity_Entity, x_] -> Rule[entity, x + 1]}
colfoo /. {{x_?NumericQ, color_} -> {x + 1, color}}

or just shifting your data range directly, you get

GeoRegionValuePlot[
 vals /. {Rule[entity_Entity, x_] -> Rule[entity, x + 1]}
 , GeoRange -> {{-60, 75}, {-130, 165}}
 , ColorFunctionScaling -> False
 , ColorFunction -> (colfoo /. {{x_?NumericQ, color_} -> {x + 1, color}})
 , GeoBackground -> Automatic
 , ImageSize -> 900
 ]

Mathematica graphics

as you wanted.

Of course, having a workaround doesn't make it any less of a reportable bug, though. But this information should make it easier to pinpoint what's broken in the first place.

$\endgroup$
2
  • $\begingroup$ Ha ha, you beat me to it by 30 seconds! I'll delete mine, but in your final bit of code you might consider adding PlotLegends -> BarLegend[{colfoo, MinMax@vals[[All, 2]]}] so as to keep the appearance of the original color scaling. $\endgroup$
    – Jason B.
    Jul 21 '16 at 14:14
  • $\begingroup$ I'm beginning to think this is the result of some clever programming optimization by WRI that assigns the first colour in Blend to negative values rather than actually calling the Blend construct to improve performance. Too clever, me'thinks. $\endgroup$
    – Edmund
    Jul 21 '16 at 16:16
0
$\begingroup$

This bug seems to affect different versions differently, so more than one workaround is needed. Here is another workaround that does not work in version 10.3.1, but it does work on other versions.

GeoRegionValuePlot[vals, GeoRange -> {{-60, 75}, {-130, 165}},
 GeoBackground -> None,
 ColorRules -> Flatten@Union[({# -> colfoo@#} & /@ vals[[All, 2]])],
 PlotLegends -> BarLegend[{colfoo, MinMax@vals[[All, 2]]}]]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.