Legend

Attributes of the legend of the ThematicMap class.

The Legend class is best accessed through the ‘legend’ attribute of the ThematicMap class.

class ContinousLegend(labels=None, pretty_labels=True, label_suffix=None, label_sep='-', rounding=None, thousand_sep=None, decimal_mark=None, **kwargs)[source]

Bases: Legend

Holds the legend attributes specific to numeric columns.

The attributes consern the labeling of the groups in the legend. Labels can be set manually with the ‘labels’ attribute, or the format of the labels can be changed with the remaining attributes.

Parameters:
  • labels (list[str] | None)

  • pretty_labels (bool)

  • label_suffix (str | None)

  • label_sep (str)

  • rounding (int | None)

  • thousand_sep (str | None)

  • decimal_mark (str | None)

labels

To manually set labels. If set, all other labeling attributes are ignored. Should be given as a list of strings with the same length as the number of color groups.

pretty_labels

If False (default), the minimum and maximum values of each color group will be used as legend labels. If True, the labels will end with the maximum value, but start at 1 + the maximum value of the previous group. The labels will be correct but inaccurate.

label_suffix

The text to put after each number in the legend labels. Defaults to None.

label_sep

Text to put in between the two numbers in each color group in the legend. Defaults to ‘-‘.

thousand_sep

Separator between each thousand for large numbers. Defaults to None, meaning no separator.

decimal_mark

Text to use as decimal point. Defaults to None, meaning ‘.’ (dot) unless ‘thousand_sep’ is ‘.’. In this case, ‘,’ (comma) will be used as decimal mark.

Examples:

Create ten random points with a numeric column from 0 to 9.

>>> import sgis as sg
>>> points = sg.random_points(10)
>>> points["number"] = range(10)
>>> points
                geometry  number
0  POINT (0.59780 0.50425)       0
1  POINT (0.07019 0.26167)       1
2  POINT (0.56475 0.15422)       2
3  POINT (0.87293 0.60316)       3
4  POINT (0.47373 0.20040)       4
5  POINT (0.98661 0.15614)       5
6  POINT (0.30951 0.77057)       6
7  POINT (0.47802 0.52824)       7
8  POINT (0.12215 0.96588)       8
9  POINT (0.02938 0.93467)       9

Creating the ThematicMap instance with a numeric column.

>>> m = sg.ThematicMap(points, column="number")

Changing the attributes that apply to both numeric and categorical columns.

>>> m.legend.title = "Meters"
>>> m.legend.position = (0.35, 0.28)

Change the attributes that only apply to numeric columns.

>>> m.label_sep = "to"
>>> m.label_suffix = "num"
>>> m.rounding = 2
>>> m.plot()

Setting labels manually. For better control, it might be wise to also set the bins manually. The following bins will create a plot with the color groups 0-2, 3-5, 6-7 and 8-9. The legend labels can then be set accordingly.

>>> m = sg.ThematicMap(points, column="number")
>>> m.bins = [0, 2, 5, 7, 9]
>>> m.legend.labels = ["0 to 2 num", "3 to 5 num", "6 to 7 num", "8 to 9 num"]
>>> m.plot()

We will get the same groups if we exclude the first and last bin values. The minimum and maximum values will be filled anyway.

>>> m = sg.ThematicMap(points, column="number")
>>> m.bins = [2, 5, 7]
>>> m.legend.labels = ["0 to 2 num", "3 to 5 num", "6 to 7 num", "8 to 9 num"]
>>> m.plot()
property rounding: int

Number of decimals in the labels.

By default, the rounding depends on the column’s maximum value and standard deviation. OBS: The bins will not be rounded, meaning the labels might be wrong if not bins are set manually.

class Legend(title=None, pretty_labels=True, labels=None, position=None, markersize=None, fontsize=None, title_fontsize=None, framealpha=1.0, edgecolor='#0f0f0f', **kwargs)[source]

Bases: object

Holds the general attributes of the legend in the ThematicMap class.

This class holds attributes of the ‘legend’ attribute of the ThematicMap class. The fontsize, title_fontsize and markersize attributes are adjusted according to the size attribute of the ThematicMap.

If a numeric column is used, additional attributes can be found in the ContinousLegend class.

Examples:

Create ten points with a numeric column from 0 to 9.

>>> import sgis as sg
>>> points = sg.to_gdf(
...     [
...         (0, 1),
...         (1, 0),
...         (1, 1),
...         (0, 0),
...         (0.5, 0.5),
...         (0.5, 0.25),
...         (0.25, 0.25),
...         (0.75, 0.75),
...         (0.25, 0.75),
...         (0.75, 0.25),
...     ]
... )
>>> points["number"] = range(10)
>>> points
                  geometry  number
0  POINT (0.00000 1.00000)       0
1  POINT (1.00000 0.00000)       1
2  POINT (1.00000 1.00000)       2
3  POINT (0.00000 0.00000)       3
4  POINT (0.50000 0.50000)       4
5  POINT (0.50000 0.25000)       5
6  POINT (0.25000 0.25000)       6
7  POINT (0.75000 0.75000)       7
8  POINT (0.25000 0.75000)       8
9  POINT (0.75000 0.25000)       9

Creating the ThematicMap instance will also create the legend. Since we pass a numeric column, a ContinousLegend is created.

>>> m = sg.ThematicMap(
...     points,
...     column="number"
...     legend_kwargs=dict(
...         title="Meters",
...         label_sep="to",
...         label_suffix="num",
...         rounding=2,
...         position = (0.35, 0.28),
...         title_fontsize=11,
...         fontsize=9,
...         markersize=7.5,
...     ),
... )
>>> m.plot()
>>> m.legend
<sgis.maps.legend.ContinousLegend object at 0x00000222206738D0>
property fontsize: int

Legend fontsize.

get(key, default=None)[source]

Get value of an attribute of the Legend.

Return type:

Any

Parameters:
  • key (Any)

  • default (Any)

property markersize: int

Legend markersize.

property position: tuple[float, float]

Legend position in x, y.

property title_fontsize: int

Legend title fontsize.

property valid_keywords: set[str]

List all valid keywords for the class initialiser.

Parameters:
  • title (str | None)

  • pretty_labels (bool)

  • labels (list[str] | None)

  • position (tuple[float] | None)

  • markersize (int | None)

  • fontsize (int | None)

  • title_fontsize (int | None)

  • framealpha (float)

  • edgecolor (str)

prettify_label(label)[source]

Replace underscores with spaces and capitalize words that are all lowecase.

Return type:

str

Parameters:

label (str)