olavolav / uniplot

Lightweight plotting to the terminal. 4x resolution via Unicode.
MIT License
343 stars 16 forks source link

Fix disappearing lines leading to overflow points. #22

Closed riga closed 4 months ago

riga commented 4 months ago

Hi @olavolav,

First of all, awesome tool 👏!

When rendering histograms, I came across cases where bars completely disappear if their height exceeds the y axis limit. Here's an example where two bars are just above the limit.

up

After a quick dive into to the pixel rendering, I think I found the issue. After the flip of the y index notation, lines that disappear have a negative y_index_start or y_index_stop. This is because the treatment interferes with how cases with y_index_stop > y_index_start were handled by a negative step size. Since the smaller and bigger indices already existed, the fix was rather trivial :)

Here is a minimal reproducer.

# coding: utf-8
from uniplot import histogram
from sklearn import datasets

d = datasets.load_iris(as_frame=True)["data"]
histogram(d["sepal length (cm)"], y_max=15)
olavolav commented 4 months ago

Thanks @riga Especially for already providing a fix. I'll look into it!

olavolav commented 4 months ago

Can confirm the problem even for a single vertical line:

plot(xs=[1,1], ys=[0,1], lines=True, y_max=0.8)

yields

┌────────────────────────────────────────────────────────────┐
││                                                           │
││                                                           │ 0.8
││                                                           │
││                                                           │
││                                                           │
││                                                           │
││                                                           │ 0.5
││                                                           │
││                                                           │
││                                                           │
││                                                           │
││                                                           │ 0.2
││                                                           │
││                             ▌                             │
││                             ▌                             │
││                             ▌                             │
││▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▌▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁│ 0.0
└────────────────────────────────────────────────────────────┘
0.0            0.5            1.0            1.5

Obviously, the line should go all the way to the top.

olavolav commented 4 months ago

@riga Thanks to your work identifying and solving this, I fixed it in commit 8a14e93. Will publish it as v0.12.3 later

It's 99% identical to your solution