nschloe / termplotlib

:chart_with_upwards_trend: Plotting on the command line
GNU General Public License v3.0
667 stars 19 forks source link

Add float formatting in `barh.py` #61

Closed ghost closed 2 years ago

ghost commented 3 years ago

This adds a formatting similar to what is already implemented for the integers when using float in barh :

Before :

Water  [3.05]  *************
Milk   [10.1]  ****************************************
Juice  [5.00756]  ********************

After:

Water  [ 3.05000]  *************
Milk   [10.10000]  ****************************************
Juice  [ 5.00756]  ********************

Each float value now uses the same width producing a better alignment of the bars.

Test script:

import termplotlib as tpl
data = ([3.05, 10.1, 5.00756], ['Water', 'Milk', 'Juice'])
fig = tpl.figure()
fig.barh(*data, force_ascii=True)
fig.show()
nschloe commented 3 years ago

all_float is not necessary, just use the else block.

ghost commented 3 years ago

Good point, fixed.

nschloe commented 3 years ago

This needs a test, too.

ghost commented 3 years ago

The code below was run with the modifications of the last commit:

python3 << 'EOF'
import termplotlib as tpl
data = ([3.05, 10.1, 5.00756], ['Water', 'Milk', 'Juice'])
fig = tpl.figure()
fig.barh(*data, force_ascii=True)
fig.show()
data = ([3, 10, 5], ['Water', 'Milk', 'Juice'])
fig = tpl.figure()
fig.barh(*data, force_ascii=True)
fig.show()
EOF
Water  [ 3.05000]  *************
Milk   [10.10000]  ****************************************
Juice  [ 5.00756]  ********************
Water  [ 3]  ************
Milk   [10]  ****************************************
Juice  [ 5]  ********************
nschloe commented 2 years ago

Closing in favor of #72.