piccolomo / plotext

plotting on terminal
MIT License
1.78k stars 83 forks source link

`plotext.show()` with no data changes subsequent plots #208

Open IgnacioJPickering opened 5 months ago

IgnacioJPickering commented 5 months ago

This is especially an issue for me when using interactive(True), since plotext.show() is automatically called each time plotext.clear_data() is called, which screws up the plots constantly, but it is also an issue with interactive(False).

An example:

Python 3.8.19 | packaged by conda-forge | (default, Mar 20 2024, 12:47:35) 
[GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import plotext
>>> plotext.__version__
'5.2.8'
>>> plotext.plot_size(50, 15)
>>> plotext.theme("clear")
>>> plotext.scatter([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> plotext.show()
    ┌────────────────────────────────────────────┐
10.0┤                                           ▝│
    │                                      ▗     │
 8.5┤                                  ▖         │
    │                                            │
 7.0┤                             ▘              │
 5.5┤                        ▘                   │
    │                   ▗                        │
 4.0┤              ▗                             │
    │                                            │
 2.5┤         ▝                                  │
    │     ▘                                      │
 1.0┤▖                                           │
    └┬──────────┬──────────┬─────────┬──────────┬┘
    1.0        3.2        5.5       7.8      10.0 
>>> plotext.clear_data()
>>> plotext.show()
┌────────────────────────────────────────────────┐
│                                                │
│                                                │
│                                                │
│                                                │
│                                                │
│                                                │
│                                                │
│                                                │
│                                                │
│                                                │
│                                                │
│                                                │
│                                                │
└────────────────────────────────────────────────┘
>>> plotext.scatter([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> plotext.show()
     ┌───────────────────────────────────────────┐
 1.00┤▘                                          │
     │                                           │
 0.67┤                                           │
     │                                           │
 0.33┤                                           │
 0.00┤                                           │
     │                                           │
-0.33┤                                           │
     │                                           │
-0.67┤                                           │
     │                                           │
-1.00┤                                           │
     └┬──────────┬─────────┬──────────┬─────────┬┘
     1.0        3.2       5.5        7.8     10.0 

Expected behavior: plot should be the same before and after plot.show() with no data.

IgnacioJPickering commented 5 months ago

I have found an ugly workaround by wrapping the methods right before calling interactive(True), which is good enough for me since I use the library for very small things, but it would be cool to fix this.

    # Monkey patch cld() and clear_data() to prevent bug
    # that happens when plotext.show() is called with no data
    plotext._clear_data = plotext.clear_data
    def _fixed_clear_data():
        if plotext._core._figure._interactive:
            plotext.interactive(False)
            plotext._clear_data()
            plotext.interactive(True)
        else:
            plotext._clear_data()
    plotext.cld = _fixed_clear_data
    plotext.clear_data = _fixed_clear_data