This page was generated from notebooks/02_legends.ipynb. Interactive online version: Binder badge

Legends in mapclassifyΒΆ

mapclassify allows for user defined formatting of legends

[1]:
import mapclassify

mapclassify.__version__
[1]:
'2.4.2+78.gc62d2d7.dirty'
[2]:
cal = mapclassify.load_example()
[3]:
q6 = mapclassify.Quantiles(cal, k=6)
q6
[3]:
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:

[4]:
q6.get_legend_classes()
[4]:
['[   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:

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

Note that this does not change the original object:

[6]:
q6
[6]:
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:

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

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