1.9. pymilestone.util.py

Module containing utility functions.

pymilestone.util.get_data_list(date_data_list, key, name_list=None)

Divides the given days into periods, where one value is constant (e.g. the month). For each period, the value itself (e.g. month index), the displayname, and the number of days in this period is determined.

This function is needed to get the top caption and top sub caption.

For example, if key is "month", then the months between the start date and the end date are determined. Furthermore, the displayname and the number of dates for each month is obtained for each month.

Parameters
  • date_data_list (list) – List, which contains the required data (year, month, week, quarter) for each day to consider.

  • key (str) – Key to consider. Possible choises are "year", "month", "week", and "quarter".

  • name_list (list of str) – Displayname for each possible entry. If None, then the respective string representation of the value (e.g. “2022” for the year 2022) is used.

Returns

data_list – List of the data for each time period (e.g. month). Each entry contains the value, displayname and number of days for this time period. Following keys are used:

Key

Type

Description

value

different

Current value (e.g. month index)

displayname

str

String representation of the value.

day

int

Number of days for the given time period.

Return type

list

pymilestone.util.set_fig_size(fig, fig_w=None, ax_w=None, fig_h=None, ax_h=None, pad=1.08, gs=None, h_pad=None, w_pad=None, max_it=10, tol=0.0001)

Sets the size of a figure. This method allows to set both the figure width/heigth or axis width/height. If the ax-width or ax-height is set, then the figure size is determined iteratively, so that the desired ax size is set.

See set_fig_size(), which should be used when the figure consists of a grid of subplots and each subplot should have the same size.

Note

If the size of the axis is set, then the size of the first axis will be tracked. This method works best, if only one axis is available.

Note

For this method, exactly one parameter of fig_w and ax_w and exactly one parameter of fig_h and ax_h must be set.

Parameters
  • fig (Figure) – Matplotlib figure of which to adjust the size.

  • fig_w (float) – Width of the figure, optional. Either fig_w or ax_w must be set.

  • ax_w (float) – Width of the axis, optional. Either fig_w or ax_w must be set.

  • fig_h (float) – Width of the figure, optional. Either fig_h or ax_h must be set.

  • ax_h (float) – Width of the axis, optional. Either fig_h or ax_h must be set.

  • pad (float) – Parameter to determine the whitespace around the axis.

  • gs (GridSpec) – Used GridSpec for the subplots. If provided, then the tight_layout method will be called on gs, utilizing the h_pad and w_pad parameters. If not provided, then the tight_layout will be called on the provided figure fig.

  • h_pad (float) – Horizontal spacing. Only used, if gs is provided.

  • w_pad (float) – Vertical spacing. Only used, if gs is provided.

  • max_it (int) – Maximum number of iterations.

  • tol (float) – Allowed tolerance for defined ax width or ax height. Allows for faster termination of the iteration.

Examples

import numpy as np
import matplotlib.pyplot as plt

from TFAFramework.plot_util import set_fig_size, activate_latex_tud

activate_latex_tud()

# 1. create test data
X = np.linspace(0, 10, 50)
y = X**2

# 2. plot data
fig = plt.figure()
ax = fig.gca()

ax.plot(X, y)
ax.set_xlabel("$x$")
ax.set_ylabel("$y$")

# 3. perform set_fig_size method

# following combinations are possible

# a) fig_w and fig_h (equivalent to calling fig.set_figwidth()
# and fig.set_figheight)
set_fig_size(fig, fig_w=4, fig_h=3, pad=0.1)

# b) ax_w and fig_h
set_fig_size(fig, ax_w=4, fig_h=3, pad=0.1)

# c) fig_w and ax_h
set_fig_size(fig, fig_w=4, ax_h=3, pad=0.1)

# d) ax_w and ax_h
set_fig_size(fig, ax_w=4, ax_h=3, pad=0.1)