Legends in mapclassifyΒΆ

mapclassify allows for user defined formatting of legends

[23]:
import mapclassify
mapclassify.__version__
[23]:
'2.3.0'
[24]:
cal = mapclassify.load_example()
[25]:
q6 = mapclassify.Quantiles(cal, k=6)
[26]:
q6
[26]:
Quantiles

     Interval        Count
--------------------------
[   0.13,    1.16] |    10
(   1.16,    3.38] |    10
(   3.38,    9.36] |     9
(   9.36,   24.32] |    10
(  24.32,   70.78] |     9
(  70.78, 4111.45] |    10

The default is to use two decimal places for this dataset. If the user desires a list of strings with these values, the get_legend_classes method can be called which will return the strings with the default format:

[27]:
q6.get_legend_classes()
[27]:
['[   0.13,    1.16]',
 '(   1.16,    3.38]',
 '(   3.38,    9.36]',
 '(   9.36,   24.32]',
 '(  24.32,   70.78]',
 '(  70.78, 4111.45]']

To set the legends to integers, an option can be passed into the method:

[28]:
q6.get_legend_classes(fmt="{:.0f}")
[28]:
['[   0,    1]',
 '(   1,    3]',
 '(   3,    9]',
 '(   9,   24]',
 '(  24,   71]',
 '(  71, 4111]']

Note that this does not change the original object:

[29]:
q6
[29]:
Quantiles

     Interval        Count
--------------------------
[   0.13,    1.16] |    10
(   1.16,    3.38] |    10
(   3.38,    9.36] |     9
(   9.36,   24.32] |    10
(  24.32,   70.78] |     9
(  70.78, 4111.45] |    10

The format can be changed on the object by calling the set_fmt method:

[30]:
q6.set_fmt(fmt="{:.0f}")
q6
[30]:
Quantiles

  Interval     Count
--------------------
[   0,    1] |    10
(   1,    3] |    10
(   3,    9] |     9
(   9,   24] |    10
(  24,   71] |     9
(  71, 4111] |    10