plotting_tools

adjust_text(texts, x=None, y=None, ax=None, expand_text=1.2, 1.2, expand_points=1.2, 1.2, autoalign=True, va='bottom', ha='center', force_text=1.0, force_points=1.0, lim=100, precision=0, only_move={}, text_from_text=True, text_from_points=True, save_steps=False, save_prefix='', save_format='png', add_step_numbers=True, draggable=True, *args, **kwargs)[source]

Iteratively adjusts the locations of texts. First moves all texts that are outside the axes limits inside. Then in each iteration moves all texts away from each other and from points. In the end hides texts and substitutes them with annotations to link them to the rescpective points.

Parameters
  • texts (list) – a list of text.Text objects to adjust

  • x (seq) – x-coordinates of points to repel from; if not provided only uses text coordinates

  • y (seq) – y-coordinates of points to repel from; if not provided only uses text coordinates

  • ax (obj) – axes object with the plot; if not provided is determined by plt.gca()

  • expand_text (seq) – a tuple/list/… with 2 numbers (x, y) to expand texts when repelling them from each other; default (1.2, 1.2)

  • expand_points (seq) – a tuple/list/… with 2 numbers (x, y) to expand texts when repelling them from points; default (1.2, 1.2)

  • autoalign (bool) – If True, the best alignment of all texts will be determined automatically before running the iterative adjustment; if ‘x’ will only align horizontally, if ‘y’ - vertically; overrides va and ha

  • va (str) – vertical alignment of texts

  • ha (str) – horizontal alignment of texts

  • force_text (float) – the repel force from texts is multiplied by this value; default 0.5

  • force_points (float) – the repel force from points is multiplied by this value; default 0.5

  • lim (int) – limit of number of iterations

  • precision (float) – up to which sum of all overlaps along both x and y to iterate; may need to increase for complicated situations; default 0, so no overlaps with anything.

  • only_move (dict) – a dict to restrict movement of texts to only certain axis. Valid keys are ‘points’ and ‘text’, for each of them valid values are ‘x’, ‘y’ and ‘xy’. This way you can forbid moving texts along either of the axes due to overlaps with points, but let it happen if there is an overlap with texts: only_move={‘points’:’y’, ‘text’:’xy’}. Default: None, so everything is allowed.

  • text_from_text (bool) – whether to repel texts from each other; default True

  • text_from_points (bool) – whether to repel texts from points; default True; can helpful to switch of in extremely crouded plots

  • save_steps (bool) – whether to save intermediate steps as images; default False

  • save_prefix (str) – a path and/or prefix to the saved steps; default ‘’

  • save_format (str) – a format to save the steps into; default ‘png

  • and **kwargs (*args) –

    any arguments will be fed into plt.annotate after all the optimization is done just for plotting

  • add_step_numbers (bool) – whether to add step numbers as titles to the images of saving steps

  • draggable (bool) – whether to make the annotations draggable; default True

get_bboxes(texts, r, expand, ax=None)[source]
get_midpoint(bbox)[source]
get_points_inside_bbox(x, y, bbox)[source]
move_texts(texts, delta_x, delta_y, bboxes=None, renderer=None, ax=None)[source]
optimally_align_text(x, y, texts, expand, renderer=None, ax=None, direction='xy')[source]

For all text objects find alignment that causes the least overlap with points and other texts and apply it

overlap_bbox_and_point(bbox, xp, yp)[source]
repel_text(texts, renderer=None, ax=None, expand=1.2, 1.2, only_use_max_min=False, move=False)[source]

Repel texts from each other while expanding their bounding boxes by expand (x, y), e.g. (1.2, 1.2) would multiply both width and height by 1.2. Requires a renderer to get the actual sizes of the text, and to that end either one needs to be directly provided, or the axes have to be specified, and the renderer is then got from the axes object.

repel_text_from_axes(texts, ax=None, bboxes=None, renderer=None, expand=None)[source]
repel_text_from_points(x, y, texts, renderer=None, ax=None, expand=1.2, 1.2, move=False)[source]

Repel texts from all points specified by x and y while expanding their (texts’!) bounding boxes by expandby (x, y), e.g. (1.2, 1.2) would multiply both width and height by 1.2. In the case when the text overlaps a point, but there is no definite direction for movement, moves in random direction by 40% of it’s width and/or height depending on Requires a renderer to get the actual sizes of the text, and to that end either one needs to be directly provided, or the axes have to be specified, and the renderer is then got from the axes object.

set_figure_settings(Figure_Type, **kwargs)[source]

Sets the figure settings for matplotlib to be either those suitable for a presentation or a paper. Updates rcParams

Parameters