Skip to main content

A native plotting widget for Textual apps

Project description

A native plotting widget for Textual apps

Textual is an excellent Python framework for building applications in the terminal, or on the web. This library provides a plot widget which your app can use to plot all kinds of quantitative data. So, no pie charts, sorry. The widget support scatter plots and line plots, and can also draw using high-resolution characters like unicode half blocks, quadrants and 8-dot Braille characters. It may still be apparent that these are drawn using characters that take up a full block in the terminal, especially when plot series overlap. However, the use of these characters can reduce the line thickness and improve the resolution tremendously.

Screenshots

screenshot of day-time spectrum

screenshot of moving sines

screenshot of error bars

screenshot of bar plot

video of plot demo

The daytime spectrum dataset shows the visible-light spectrum recorded by an Ocean Optics USB2000+ spectrometer using the DeadSea Optics software. It was taken in the morning while the detector was facing my office window.

Features

  • Line plots
  • Scatter plots
  • Scatter plots with errorbars
  • Bar plots, also for categorical data
  • Vertical lines
  • Automatic scaling and tick placement at nice intervals (1, 2, 5, etc.)
  • Axes labels
  • Axis formatters for numbers, durations and categories
  • Support for custom axis formatters
  • High-resolution modes using unicode half blocks (1x2), quadrants (2x2) and braille (2x8) characters
  • Mouse support for zooming (mouse scrolling) and panning (mouse dragging)
  • Horizontal- or vertical-only zooming and panning when the mouse cursor is in the plot margins
  • Keyboard bindings for zooming, panning, and resetting the scale.

Running the demo / installation

Using uv:

uvx textual-plot

Using pipx:

pipx run textual-plot

Install the package with pip (please, use virtual environments):

pip install textual-plot

Tutorial

A minimal example is shown below: screenshot of minimal example

from textual.app import App, ComposeResult

from textual_plot import PlotWidget


class MinimalApp(App[None]):
    def compose(self) -> ComposeResult:
        yield PlotWidget()

    def on_mount(self) -> None:
        plot = self.query_one(PlotWidget)
        plot.plot(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])


MinimalApp().run()

You include a PlotWidget in your compose method and after your UI has finished composing, you can start plotting data. The plot() method takes x and y data which should be array-like. It can be lists, or NumPy arrays, or really anything that can be turned into a NumPy array which is what's used internally. The plot() method further accepts a line_style argument which accepts Textual styles like "white", "red on blue3", etc. For standard low-resolution plots, it does not make much sense to specify a background color since the text character used for plotting is a full block filling an entire cell.

A longer tutorial can be found at here.

Reference

A reference of all available methods can be found here.

Alternatives

Textual-plotext uses the plotext library which has more features than this library. However, it does not support interactive zooming or panning and the tick placement isn't as nice since it simply divides up the axes range into a fixed number of intervals giving values like 0, 123.4, 246.8, etc. The plotext library hasn't been updated since September, 2024 and hadn't seen significant updates since December, 2023. The author is working on a new version, a full rewrite, since at least October 2023. If you're looking for a library to use for your Textual application, this is it.

Roadmap

We'll work on adding some features like datetime axes, and some quality-of-life improvements like even tick spacing. This will (probably) not turn into a general do-it-all plotting library. We focus first on handling quantitative data in the context of physics experiments. If you'd like to see features added, do let us know. And if a PR is of good quality and is a good fit for the API, we'd love to handle more use cases beyond physics. And who knows, maybe this will turn into a general plotting library!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textual_plot-0.10.0.post1.tar.gz (2.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

textual_plot-0.10.0.post1-py3-none-any.whl (50.2 kB view details)

Uploaded Python 3

File details

Details for the file textual_plot-0.10.0.post1.tar.gz.

File metadata

  • Download URL: textual_plot-0.10.0.post1.tar.gz
  • Upload date:
  • Size: 2.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for textual_plot-0.10.0.post1.tar.gz
Algorithm Hash digest
SHA256 dcd257477dbaf67aed514b964f6e9301611e796a41fd4c9dc0aece080571510b
MD5 2fb38e035fff19b234dc1dee607a7054
BLAKE2b-256 ef4d813c4072392bcbe8ada1222a6f743fc3cbbf970d267957670d4da298d048

See more details on using hashes here.

File details

Details for the file textual_plot-0.10.0.post1-py3-none-any.whl.

File metadata

  • Download URL: textual_plot-0.10.0.post1-py3-none-any.whl
  • Upload date:
  • Size: 50.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for textual_plot-0.10.0.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 08a281c114599cd9bf473fc809ccda607d3b725473b93735793fc2470cf3b67f
MD5 c2d47b31f4fb734269b577db9e25bf9c
BLAKE2b-256 b7521e3d02200e09714ac3f3bc2b077f6c411a439ebc9e99ff69c698c5b1fcda

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page