From 238ce173b7bd192c1e4eded67c3ce68edbd54e11 Mon Sep 17 00:00:00 2001 From: Jan Pipek Date: Tue, 25 Feb 2025 15:23:49 +0100 Subject: [PATCH 1/2] plotext basics --- src/physt/plotting/plotext.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/physt/plotting/plotext.py diff --git a/src/physt/plotting/plotext.py b/src/physt/plotting/plotext.py new file mode 100644 index 0000000..96a7417 --- /dev/null +++ b/src/physt/plotting/plotext.py @@ -0,0 +1,35 @@ +import typing + +import plotext as plt + +if typing.TYPE_CHECKING: + from physt.types import Histogram1D + + +types: typing.Tuple[str, ...] = ("bar", "hbar") + + +dims = { + "hbar": [1], + "bar": [1], +} + + +def hbar( + h1: "Histogram1D", +) -> None: + fig = plt.active() + x = h1.bin_centers + y = h1.frequencies + fig.monitor.draw_bar(x, y, orientation="horizontal") + plt.show() + + +def bar( + h1: "Histogram1D", +) -> None: + fig = plt.active() + x = h1.bin_centers + y = h1.frequencies + fig.monitor.draw_bar(x, y, orientation="vertical") + plt.show() From 0a19fd3b12e6e88dd02a44e0c8048246a642c57e Mon Sep 17 00:00:00 2001 From: Jan Pipek Date: Tue, 25 Feb 2025 16:00:49 +0100 Subject: [PATCH 2/2] plotext changes --- src/physt/examples/__main__.py | 4 +++- src/physt/plotting/__init__.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/physt/examples/__main__.py b/src/physt/examples/__main__.py index d679e3c..df082c8 100644 --- a/src/physt/examples/__main__.py +++ b/src/physt/examples/__main__.py @@ -4,7 +4,7 @@ python -m physt.examples """ -import importlib +import importlib.util import sys from rich.json import JSON @@ -37,6 +37,8 @@ h1.plot(backend="ascii", show_values=True, color="green") console.print() +h1.plot(backend="plotext", kind="hbar") + console.print("[bold]JSON fully describing the histogram:[/bold]") console.print(Panel(JSON(h1.to_json()), width=min(console.width, 60))) diff --git a/src/physt/plotting/__init__.py b/src/physt/plotting/__init__.py index faea91c..a563db8 100644 --- a/src/physt/plotting/__init__.py +++ b/src/physt/plotting/__init__.py @@ -136,6 +136,12 @@ backends["folium"] = folium_backend +with suppress(KeyError): + from . import plotext as plotext_backend + + backends["plotext"] = plotext_backend + + backends["ascii"] = ascii_backend _default_backend: Optional[str] = None