Ejemplo n.º 1
0
def single_run():
    x_min = -5 * random.random() + 1
    x_max = 1_000 * random.random() + 1.5
    space = random.randint(5, 200)
    is_vertical = int(random.random() < 0.5)

    extended_talbot_labels(x_min=x_min,
                           x_max=x_max,
                           available_space=space,
                           vertical_direction=is_vertical)
Ejemplo n.º 2
0
def test_typical_labeling_case_with_small_range():
    """
    This is the result when plotting [1, 2, 3] using the default settings. We are testing both axis here.
    """
    ls = extended_talbot_labels(x_min=0.97,
                                x_max=3.03,
                                available_space=60,
                                vertical_direction=False)
    assert np.array_equal(ls.labels, [1.0, 2.0, 3.0])

    ls = extended_talbot_labels(x_min=0.97,
                                x_max=3.03,
                                available_space=17,
                                vertical_direction=True)
    assert np.array_equal(ls.labels, [1.0, 2.0, 3.0])
Ejemplo n.º 3
0
def _generate_body_raw_elements(
        series: MultiSeries,
        options: Options) -> Tuple[str, List[str], np.array]:
    """
    Generates the x-axis labels, y-axis labels, and the pixel character matrix.
    """
    # Prepare y axis labels
    y_axis_label_set = extended_talbot_labels(
        x_min=options.y_min,
        x_max=options.y_max,
        available_space=options.height,
        unit=options.y_unit,
        vertical_direction=True,
    )
    y_axis_labels = [""] * options.height
    if y_axis_label_set is not None:
        y_axis_labels = y_axis_label_set.render()

    # Observe line_length_hard_cap
    if options.line_length_hard_cap is not None:
        options.reset_width()
        # Determine maximum length of y axis label
        max_y_label_length = max([len(l) for l in y_axis_labels])
        # Make sure the total plot does not exceed `line_length_hard_cap`
        if 2 + options.width + 1 + max_y_label_length > options.line_length_hard_cap:
            # Overflow, so we need to reduce width of plot area
            options.width = options.line_length_hard_cap - (2 + 1 +
                                                            max_y_label_length)
            if options.width < 1:
                raise

    # Prepare x axis labels
    x_axis_label_set = extended_talbot_labels(
        x_min=options.x_min,
        x_max=options.x_max,
        available_space=options.width,
        unit=options.x_unit,
        vertical_direction=False,
    )
    x_axis_labels = ""
    if x_axis_label_set is not None:
        x_axis_labels = x_axis_label_set.render()[0]

    # Prefare graph surface
    pixel_character_matrix = layer_assembly.assemble_scatter_plot(
        xs=series.xs, ys=series.ys, options=options)

    return (x_axis_labels, y_axis_labels, pixel_character_matrix)
Ejemplo n.º 4
0
def test_vertical_labelling_case_of_some_konux_displacement_data():
    ls = extended_talbot_labels(
        x_min=-0.1089490667533125,
        x_max=1.1452251006076242,
        available_space=17,
        vertical_direction=True,
    )
    assert 2 <= len(ls.labels) < 5
Ejemplo n.º 5
0
def test_simple_labeling_case():
    ls = extended_talbot_labels(x_min=-0.5,
                                x_max=2.5,
                                available_space=40,
                                vertical_direction=False)
    assert 0.0 in ls.labels
Ejemplo n.º 6
0
def plot(ys: Any, xs: Optional[Any] = None, **kwargs) -> None:
    """
    2D scatter dot plot on the terminal.

    Parameters:

    - `ys` are the y coordinates of the points to plot. This parameter is mandatory and
      can either be a list or a list of lists, or the equivalent NumPy array.
    - `xs` are the x coordinates of the points to plot. This parameter is optional and
      can either be a `None` or of the same shape as `ys`.
    - Any additional keyword arguments are passed to the `uniplot.options.Options` class.
    """
    series = MultiSeries(xs=xs, ys=ys)
    options = validate_and_transform_options(series=series, kwargs=kwargs)

    # Print title
    if options.title is not None:
        print(elements.plot_title(options.title, width=options.width))

    # Main loop for interactive mode. Will only be executed once when not in interactive # mode.
    continue_looping: bool = True
    loop_iteration: int = 0
    while continue_looping:
        # Make sure we stop after first iteration when not in interactive mode
        if not options.interactive:
            continue_looping = False

        # Prepare y axis labels
        y_axis_labels = ["-error generating labels, sorry-"] * options.height
        y_axis_label_set = extended_talbot_labels(
            x_min=options.y_min,
            x_max=options.y_max,
            available_space=options.height,
            vertical_direction=True,
        )
        if y_axis_label_set is not None:
            y_axis_labels = y_axis_label_set.render()

        # Prepare x axis labels
        x_axis_label_set = extended_talbot_labels(
            x_min=options.x_min,
            x_max=options.x_max,
            available_space=options.width,
            vertical_direction=False,
        )
        x_axis_labels = "-error generating labels, sorry-"
        if x_axis_label_set is not None:
            x_axis_labels = x_axis_label_set.render()[0]

        # Prefare graph surface
        pixel_character_matrix = layer_assembly.assemble_scatter_plot(
            xs=series.xs, ys=series.ys, options=options)

        # Delete plot before we re-draw
        if loop_iteration > 0:
            nr_lines_to_erase = options.height + 4
            if options.legend_labels is not None:
                nr_lines_to_erase += len(options.legend_labels)
            elements.erase_previous_lines(nr_lines_to_erase)

        # Print plot (double resolution)
        print(f"┌{'─'*options.width}┐")
        for i in range(options.height):
            row = pixel_character_matrix[i]
            print(f"│{''.join(row)}│ {y_axis_labels[i]}")
        print(f"└{'─'*options.width}┘")
        print(x_axis_labels)

        # Print legend if labels were specified
        # TODO Fix erase during interactive mode
        if options.legend_labels is not None:
            print(elements.legend(options.legend_labels, width=options.width))

        if options.interactive:
            print("Move h/j/k/l, zoom u/n, or r to reset. ESC/q to quit")
            key_pressed = getch().lower()

            # TODO Move all of the below to the `Options` class
            if key_pressed == "h":
                # Left
                step = 0.1 * (options.x_max - options.x_min)
                options.x_min = options.x_min - step
                options.x_max = options.x_max - step
            elif key_pressed == "l":
                # Right
                step = 0.1 * (options.x_max - options.x_min)
                options.x_min = options.x_min + step
                options.x_max = options.x_max + step
            elif key_pressed == "j":
                # Up
                step = 0.1 * (options.y_max - options.y_min)
                options.y_min = options.y_min - step
                options.y_max = options.y_max - step
            elif key_pressed == "k":
                # Down
                step = 0.1 * (options.y_max - options.y_min)
                options.y_min = options.y_min + step
                options.y_max = options.y_max + step
            elif key_pressed == "u":
                # Zoom in
                step = 0.1 * (options.x_max - options.x_min)
                options.x_min = options.x_min + step
                options.x_max = options.x_max - step
                step = 0.1 * (options.y_max - options.y_min)
                options.y_min = options.y_min + step
                options.y_max = options.y_max - step
            elif key_pressed == "n":
                # Zoom out
                step = 0.1 * (options.x_max - options.x_min)
                options.x_min = options.x_min - step
                options.x_max = options.x_max + step
                step = 0.1 * (options.y_max - options.y_min)
                options.y_min = options.y_min - step
                options.y_max = options.y_max + step
            elif key_pressed == "r":
                options.reset_view()
            elif key_pressed in ["q", "\x1b"]:
                # q and Escape will end interactive mode
                continue_looping = False

            loop_iteration += 1
Ejemplo n.º 7
0
from uniplot.axis_labels.extended_talbot_labels import extended_talbot_labels

x_min = 6.5
x_max = 7.5
for i in range(150):
    x_max = x_max * 1.05
    space = 60
    is_vertical = False

    ls = extended_talbot_labels(x_min=x_min,
                                x_max=x_max,
                                available_space=space,
                                vertical_direction=is_vertical)
    print(ls.render()[0])