Skip to content

bs_plots module

General plotting utilities.

  • set_axis: set the axis limits, with a margin
  • drawArrow_2dim: draw an arrow between two points in a 2D Matplotlib plot.

drawArrow_2dim(ax, xA, xB, yA, yB, c='k', ls='-')

Draw an arrow between two points in a 2D Matplotlib plot.

Parameters:

Name Type Description Default
ax Axes

the axis object

required
xA float

the x coordinate of the starting point

required
xB float

the x coordinate of the ending point

required
yA float

the y coordinate of the starting point

required
yB float

the y coordinate of the ending point

required
c str

the color of the arrow

'k'
ls str

the line style of the arrow

'-'

Returns:

Type Description
None

nothing

Source code in bs_python_utils/bs_plots.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def drawArrow_2dim(
    ax: axes.Axes,
    xA: float,
    xB: float,
    yA: float,
    yB: float,
    c: str = "k",
    ls: str = "-",
) -> None:
    """Draw an arrow between two points in a 2D Matplotlib plot.

    Args:
        ax: the axis object
        xA: the x coordinate of the starting point
        xB: the x coordinate of the ending point
        yA: the y coordinate of the starting point
        yB: the y coordinate of the ending point
        c: the color of the arrow
        ls: the line style of the arrow

    Returns:
        nothing
    """
    n = 50
    x = np.linspace(xA, xB, 2 * n + 1)
    y = np.linspace(yA, yB, 2 * n + 1)
    ax.plot(x, y, color=c, linestyle=ls)
    ax.annotate(
        "",
        xy=(x[n], y[n]),
        xytext=(x[n - 1], y[n - 1]),
        arrowprops={"arrowstyle": "-|>", "color": c},
        size=15,
        # zorder=2,
    )

set_axis(x, margin=0.05)

sets the axis limits with a margin

Parameters:

Name Type Description Default
x ndarray

the values of the variable

required
margin float

the margin to add, a fraction of the range of the variable

0.05

Returns:

Type Description
tuple[float, float]

the min and max for the axis.

Source code in bs_python_utils/bs_plots.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def set_axis(x: np.ndarray, margin: float = 0.05) -> tuple[float, float]:
    """sets the axis limits with a margin

    Args:
        x: the values of the variable
        margin: the margin to add, a fraction of the range of the variable

    Returns:
        the min and max for the axis.
    """
    x_min, x_max = x.min(), x.max()
    scaled_diff = margin * (x_max - x_min)
    x_min -= scaled_diff
    x_max += scaled_diff
    return x_min, x_max