The text plot module

Autogenerated API documentation for text_plot

GNU plot based plots direcly into a curses window

This module provides two classes for creating text plots, one for creating a text plot as a text string AsciiPlot and one for writing the resulting plot directly into a curses window, CursesAsciiPlot, automatically adjusting the size of the plot to the window.

AsciiPlot example:

from PyExpLabSys.common.text_plot import AsciiPlot
import numpy

x = numpy.linspace(0, 6.28, 100)
y = numpy.sin(x)

ascii_plot = AsciiPlot(title='Sine', xlabel='x', ylabel='y', size=(80, 24))
text_plot = ascii_plot.plot(x, y)
print(text_plot)

Produces the following output:

                                      Sine

    1 +--------------------------------------------------------------------+
      |         ***      ***        +        +         +         +         |
  0.8 |-+     **            **                                           +-|
  0.6 |-+   ***               *                                          +-|
      |    **                  **                                          |
  0.4 |-+ *                     **                                       +-|
      |  *                        *                                        |
  0.2 |**                          *                                     +-|
    0 |*+                           **                                   +-|
y     |                              **                            **      |
 -0.2 |-+                              *                          **     +-|
      |                                 *                        *         |
 -0.4 |-+                                **                     *        +-|
      |                                   **                  **           |
 -0.6 |-+                                   *               ***          +-|
 -0.8 |-+                                    **            **            +-|
      |         +         +         +        + ***     +***      +         |
   -1 +--------------------------------------------------------------------+
      0         1         2         3        4         5         6         7
                                        x

CursesAsciiPlot example

import time
import curses
import numpy as np
from PyExpLabSys.common.text_plot import CursesAsciiPlot

# Init and clear
stdscr = curses.initscr()
stdscr.clear()
curses.noecho()

# Make plot window
screen_size = stdscr.getmaxyx()
# Make the plot a little smaller than the main window, to allow a
# littel space for text at the top
win = curses.newwin(screen_size[0] - 3, screen_size[1], 3, 0)

t_start = time.time()
try:
    # Create the Curses Ascii Plotter
    ap = CursesAsciiPlot(
        win, title="Log of sine of time + 1.1", xlabel="Time [s]",
        logscale=True,
    )

    # Plot the sine to time since start and 10 sec a head
    while True:
        stdscr.clear()
        t0 = time.time() - t_start
        # Write the time right now to the main window
        stdscr.addstr(1, 3, 'T0: {:.2f}       '.format(t0))
        stdscr.refresh()
        x = np.linspace(t0, t0 + 10)
        y = np.sin(x) + 1.1
        ap.plot(x, y, legend="Sine")
        time.sleep(0.2)
finally:
    curses.echo()
    curses.endwin()
class PyExpLabSys.common.text_plot.CursesAsciiPlot(curses_win, **kwargs)[source]

Bases: object

A Curses Ascii Plot

__init__(curses_win, **kwargs)[source]

Initialize local variables

Parameters:curses_win (curses-window-objects) – The curses window to print the plot into

For possible value for kwargs, see arguments for AsciiPlot.__init__().

plot(*args, **kwargs)[source]

Plot data to the curses window

For an explanation of the arguments, see AsciiPlot.plot().

class PyExpLabSys.common.text_plot.AsciiPlot(title=None, xlabel=None, ylabel=None, logscale=False, size=(80, 24), debug=False)[source]

Bases: object

An Ascii Plot

__init__(title=None, xlabel=None, ylabel=None, logscale=False, size=(80, 24), debug=False)[source]

Initialize local varibles

Parameters:
  • title (str) – The title of the plot if required
  • xlabel (str) – The xlabel of the plot if required
  • ylabel (str) – The ylabel of the plot if required
  • logscale (bool) – If the yaxis should use log scale
  • size (tuple) – A list or tuple with two integers indication the x and y size (i.e. number of columns and lines) of the plot
  • debug (bool) – Whether to show the command sent to gnuplot
write(string)[source]

Write string to gnuplot

String must be n terminated

plot(x, y, style='lines', legend='')[source]

Plot data

Parameters:
  • x (iterable) – An iterable of floats or ints to plot
  • y (iterable) – An iterable of floats or ints to plot
  • style (str) – ‘lines’ or ‘points’
  • legend (str) – The legend of the data (leave to empty string to skip)