pyppin.math.plot_ascii

[View Source]

Generate ASCII-art plots of functions and data.

Functions

plot_ascii(data[, width, height, x_axis, ...])

Create an ASCII-art plot of some numerical data.

Classes

AxisOptions([min, max, labels])

Information about an axis of the plot

Canvas(width, height, x_range, y_range[, ...])

A Canvas is an object for drawing ASCII plots.

pyppin.math.plot_ascii.plot_ascii(data: Callable[[float], float], width: int = 100, height: int = 0, x_axis: Optional[AxisOptions] = None, y_axis: Optional[AxisOptions] = None, vfill: bool = False, plot_symbol: str = '#') str[source]

Create an ASCII-art plot of some numerical data.

Parameters
  • data – The data to be plotted. If you want to plot a list or dict of data, rather than a function, use the helpers in pyppin.math.functions to convert them.

  • width – The size, in characters, of the output to generate. The height will default to half the width.

  • height – The size, in characters, of the output to generate. The height will default to half the width.

  • x_axis – Options for the axes.

  • y_axis – Options for the axes.

  • vfill – If True, show a vertical fill (in the style of a bar chart) below each value. If False, show the values as points.

  • plot_symbol – The char to use when plotting it.

Returns

Beautiful ASCII art, like this:

 >>> print(plot_ascii(math.cos, width=80, x_axis=AxisOptions(min=0, max=10), y_axis=AxisOptions(min=-1.5, max=1.5)))
 1.5 +
     |
     |
     |
     |
 1.1 +
     |
     |###                                         #####
     |   ##                                     ##     #
     |     #                                   #        #
 0.7 +                                        #          #
     |      #                                             #
     |       #                               #             #
     |        #                             #
     |                                                      #
 0.3 +         #                           #                 #
     |                                    #
     |          #                                             #
     |           #                       #                     #
     |
-0.1 +            #                     #                       #
     |                                 #
     |             #                                             #
     |              #                 #                           #
     |
-0.5 +               #               #                             #
     |                #             #                               #
     |                 #           #
     |                            #                                  #
     |                  #        #                                    #        #
-0.9 +                   ##     #                                      ##     #
     |                     #####                                         #####
     |
     |
     |
-1.3 +
     |
     |
     ++----+----+----+----+----+----+----+----+----+----+----+----+----+----+---
      0.0  0.7  1.4  2.1  2.7  3.4  4.1  4.8  5.5  6.2  6.8  7.5  8.2  8.9  9.6

class pyppin.math.plot_ascii.AxisOptions(min: Optional[float] = None, max: Optional[float] = None, labels: Optional[Dict[float, str]] = None)[source]

Bases: NamedTuple

Information about an axis of the plot

min: Optional[float][source]

The minimum value to show on the plot, or None to infer from data.

max: Optional[float][source]

The maximum value to show on the plot, or None to infer from data.

labels: Optional[Dict[float, str]][source]

Axis labels, or None to infer from data.

class pyppin.math.plot_ascii.Canvas(width: int, height: int, x_range: Tuple[float, float], y_range: Tuple[float, float], x_labels: Optional[Dict[float, str]] = None, y_labels: Optional[Dict[float, str]] = None, background: str = ' ')[source]

Bases: object

A Canvas is an object for drawing ASCII plots.

It works in terms of three coordinate systems: - Natural coordinates (float) are the X and Y values of the function itself, with the origin

at the lower left.

  • Image coordinates (int) are pixel positions relative to the image region of the output, i.e. the sub-box inside the axes, with the origin at the lower left.

  • Screen coordinates (int) are pixel positions within the full width ⊗ height grid, with the origin at the upper left.

Parameters
  • width – The dimensions in pixels (ie chars) of the output image.

  • height – The dimensions in pixels (ie chars) of the output image.

  • x_range – The X and Y axis ranges, in natural coordinates.

  • y_range – The X and Y axis ranges, in natural coordinates.

  • x_labels – Optional dicts from natural coordinate to label for the axes.

  • y_labels – Optional dicts from natural coordinate to label for the axes.

  • background – The background symbol for the plot.

If the labels aren’t given, they will be inferred. If you want no labels, pass an explicit empty dict.

classmethod for_plot(data: Callable[[float], float], width: int, height: int, x_axis: Optional[AxisOptions] = None, y_axis: Optional[AxisOptions] = None, background: str = ' ') Canvas[source]

Create a Canvas suited to displaying this plot on its own.

Parameters
  • data – The function you are going to plot.

  • width – The dimensions of the output canvas.

  • height – The dimensions of the output canvas.

  • x_axis – Per-axis options.

  • y_axis – Per-axis options.

  • background – The background char for the plot.

natural_to_image_x(arg: float) int[source]

Convert natural coordinates to image coordinates on the X axis.

natural_to_image_y(arg: float) int[source]

Convert natural coordinates to image coordinates on the Y axis.

image_to_natural_x(arg: int) float[source]

Convert image to natural coordinates on the X axis.

image_to_natural_y(arg: int) float[source]

Convert image to natural coordinates on the Y axis.

image_to_screen_x(arg: int) int[source]

Convert image to screen coordinates on the X axis.

image_to_screen_y(arg: int) int[source]

Convert image to screen coordinates on the Y axis.

screen_to_image_x(arg: int) int[source]

Convert screen to image coordinates on the X axis.

screen_to_image_y(arg: int) int[source]

Convert screen to image coordinates on the Y axis.

natural_to_screen_x(arg: float) int[source]

Convert natural coordinates to screen coordinates on the X axis.

natural_to_screen_y(arg: float) int[source]

Convert natural coordinates to screen coordinates on the Y axis.

pixel(image_x: int, image_y: int) str[source]
set_pixel(image_x: int, image_y: int, char: str) None[source]
plot(data: Callable[[float], float], vfill: bool, symbol: str) None[source]

Add a function plot on top of the canvas.

Parameters
  • data – The function to plot.

  • vfill – If true, add vertical “fill” lines (like in a bar graph) below the curve.

  • symbol – The character to use for the plot line.

scatter_plot(data: List[Tuple[float, float]], symbol: str) None[source]

Add a scatter plot of (x, y) pairs.

render() str[source]

Actually generate the output plot.