What would a minimal example for a choropleth map in Mathematica look like?

后端 未结 5 827
抹茶落季
抹茶落季 2021-02-06 11:19

What would a minimal example for a choropleth map in Mathematica look like?

I can read in a ESRI Shapefile using Import, but do not know how to work with th

相关标签:
5条回答
  • 2021-02-06 11:49

    Because I cannot resist a Code Golf competition with belisarius:

    Graphics[{Hue[i~#~"Area"/10^7],i~#~"Polygon"}~Table~{i,#[]}&@CountryData]
    

    (for the same result)

    0 讨论(0)
  • 2021-02-06 11:52
    Graphics[
       {
        ColorData["ThermometerColors"][
                                Rescale[CountryData[#, "GDPPerCapita"], {100, 50000}]
                                      ] /. HoldPattern[Blend[___]] -> Yellow, 
        CountryData[#, "Polygon"]
       } & /@
       CountryData[]
    ]
    

    enter image description here

    And why the replacement? If there are no data of the required type for a given country CountryData returns Missing["NotAvailable"], causing ColorData, and its underlying Blend function not to return a specific RGB value. I replace this unevaluated Blend with the color Yellow.

    0 讨论(0)
  • 2021-02-06 11:59

    Just for reference, here some tips for working with ESRI Shapefiles. CountryData does not provide county-level data for Germany (the administrative unit is called "Kreis"), which is why I wrote my own KreisData function. The shape file I used can be downloaded for free, however there are terms of use to consider.

    The KreisData function is then created as follows:

    shp = Import["C:/TEMP/map/VG2500/vg2500_krs.shp", "Data"];
    polys = "Geometry" /. First[shp];
    ags = "RS" /. ("LabeledData" /. First[shp]);
    names = "GEN" /. ("LabeledData" /. First[shp]);
    area = "SHAPE_AREA" /. ("LabeledData" /. First[shp]);
    KreisDataRules = 
      Dispatch[MapThread[
        Rule[#1, #2] &, {ags, Transpose[{polys, area, names}]}]];
    KreisData[tag_String, "Polygon"] := First[tag /. KreisDataRules];
    KreisData[tag_String, "Area"] := Part[tag /. KreisDataRules, 2];
    KreisData[tag_String, "Name"] := Last[tag /. KreisDataRules];
    KreisData[] := ags;
    

    With this function, and the example code by Sjoerd C. de Vries, a map of Germany is created thus:

    renderMap[scheme_String] :=
      Graphics[{ColorData[scheme][
            Rescale[KreisData[#, "Area"], {3.63067036816521*10^7, 
              3.08469540395003*10^9}]] /. 
           HoldPattern[Blend[___]] -> Yellow, KreisData[#, "Polygon"]} & /@
         KreisData[]];
    Manipulate[renderMap[s], {s, ColorData["Gradients"]}]
    

    Result with "GrayTones" color scheme

    0 讨论(0)
  • 2021-02-06 12:00

    A throw on Minimal in the code golf sense:

    Graphics@Function[f,{Hue[f[#,"Area"]/10^7],f[#,"Polygon"]} &/@ f[]]@CountryData
    

    enter image description here

    0 讨论(0)
  • 2021-02-06 12:05

    @Karsten W.: unfortunately your shp file is not longer available. I tried a similar one (vg250_0101.utm32s.shape.ebenen\vg250_ebenen\vg250_krs.shp) from the same source but I got the error message "Transpose: the first two levels can not be transposed" from your function KreisDataRules. And since I did not really understand what your code does, maybe you can help. I try to produce a thematic map on a Kreis level, where the colouring shows the number of tourists in our city, if necessary also 0. I would be very grateful for any help.

    0 讨论(0)
提交回复
热议问题