Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple bars

Tags:

python

tqdm

I would like to have two independent progress bars.

This is a minimal example where if I use two bars they are not updated properly. Instead, new bars are created.

import time
from tqdm import *

pbar1 = tqdm(total=100)
pbar2 = tqdm(total=200)

for i in range(10):
    pbar1.update(10)
    pbar2.update(20)
    time.sleep(1)

When running the example. I get something like:

  0%|          | 0/100 [00:00<?, ?it/s]
 20%|██        | 20/100 [00:01<00:04, 19.97it/s]
 30%|███       | 30/100 [00:02<00:04, 15.36it/s]
 40%|████      | 40/100 [00:03<00:04, 13.23it/s]
 50%|█████     | 50/100 [00:04<00:04, 12.05it/s]
 60%|██████    | 60/100 [00:05<00:03, 11.35it/s]
 70%|███████   | 70/100 [00:06<00:02, 10.90it/s]
 80%|████████  | 80/100 [00:07<00:01, 10.61it/s]
 90%|█████████ | 90/100 [00:08<00:00, 10.42it/s]
100%|██████████| 100/100 [00:09<00:00, 10.28it/s]
100%|██████████| 200/200 [00:09<00:00, 20.57it/s]

As shown, updating does not work properly and adds new bars. Is it possible to have two independent bars that update properly?

like image 401
Juan Leni Avatar asked Aug 17 '17 18:08

Juan Leni


People also ask

When would you use a multiple bar graph?

The use of Multi-set Bar Charts is usually to compare grouped variables or categories to other groups with those same variables or category types. Multi-set Bar Charts can also be used to compare mini Histograms to each other, so each bar in the group would represent the significant intervals of a variable.

What is a multiple bar?

A multiple bar graph is a graph that has multiple bars for each category.A multiple bar graph compares as many sets of data you want. The process for creating a multiple bar graph is just like creating any other bar graph, only you will have more colors to represent different sets of data.

What do you call a bar graph with multiple bars?

What is a grouped bar chart? A grouped bar chart (aka clustered bar chart, multi-series bar chart) extends the bar chart, plotting numeric values for levels of two categorical variables instead of one.


2 Answers

Try using the position parameter when initialising the bars:

pbar1 = tqdm(total=100, position=1)
pbar2 = tqdm(total=200, position=0)

From the tqdm GitHub page:

position : int, optional

Specify the line offset to print this bar (starting from 0) Automatic if unspecified. Useful to manage multiple bars at once (eg, from threads).

like image 124
Aman Singh Avatar answered Oct 20 '22 07:10

Aman Singh


Here I have some examples of nested progress bar, examples of tqdm and some issues I have with it in general; from which I highlight the following code snippet that yields to two nice nested progress bars.

def test48():
  with tqdm.notebook.trange(3, position=0, desc='Outter') as outter_range:
    for i in outter_range:
      leave = i == len(outter_range) - 1
      for _ in tqdm.notebook.trange(3, position=1, leave=leave, desc='Inner'):
        sleep(.3)

enter image description here

like image 4
Eduardo Reis Avatar answered Oct 20 '22 08:10

Eduardo Reis