Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotting Text Crashes on Second show() Call #94

Closed
Samiisd opened this issue May 29, 2022 · 3 comments
Closed

Plotting Text Crashes on Second show() Call #94

Samiisd opened this issue May 29, 2022 · 3 comments

Comments

@Samiisd
Copy link

Samiisd commented May 29, 2022

Description

The second call to plt.show after that plt.text was used generates a list out of range exception

Reproduce

Let's take the example code from the documentation for plt.text:

import plotext as plt

pizzas = ["Sausage", "Pepperoni", "Mushrooms", "Cheese", "Chicken", "Beef"]
percentages = [14, 36, 11, 8, 7, 4]

plt.bar(pizzas, percentages)
plt.title("Labelled Bar Plot using Text()")

[plt.text(pizzas[i], x = pizzas[i], y = percentages[i] + 0.8, alignment = 'center', color = 'red') for i in range(len(pizzas))]
plt.ylim(0, 38)
plt.show()

# ERROR: this second call generates the `list index out of range exception`
plt.show() 

Traceback

IndexError                                Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 plt.show()

File ~/.local/lib/python3.10/site-packages/plotext/_core.py:183, in show()
    182 def show():
--> 183     figure.show()

File ~/.local/lib/python3.10/site-packages/plotext/_figure.py:250, in _figure_class.show(self)
    248 def show(self): # it shows the current figure
    249     t = time()
--> 250     self.build()
    251     ut.write(self.monitor.canvas)
    252     self._time = time() - t

File ~/.local/lib/python3.10/site-packages/plotext/_figure.py:257, in _figure_class.build(self)
    255 def build(self): # it build the current figure without showing it
    256     self._set_sizes()
--> 257     self._build_matrix()
    258     self.monitor.to_canvas()
    259     return self.monitor.canvas

File ~/.local/lib/python3.10/site-packages/plotext/_figure.py:283, in _figure_class._build_matrix(self)
    281 def _build_matrix(self):
    282     if self._no_plots:
--> 283         self.monitor.build_plot(*self._size) if not self.monitor.fast_plot else None
    284     else:
    285         [[self._get_subplot(row, col)._build_matrix() for col in self._Cols] for row in self._Rows]

File ~/.local/lib/python3.10/site-packages/plotext/_monitor.py:668, in monitor_class.build_plot(self, width, height)
    665 axes_ansi = ut.all_ansi(*axes_colors)
    667 # Apply Axes Scale (linear or log)
--> 668 xscale = [self.xscale[0] if self.xside[s] == self.default.xside[0] else self.xscale[1] for s in Signals]
    669 yscale = [self.yscale[0] if self.yside[s] == self.default.yside[0] else self.yscale[1] for s in Signals]
    670 self.x = [self.x[s] if xscale[s] is self.default.xscale[0] else ut.log(self.x[s]) for s in Signals]

File ~/.local/lib/python3.10/site-packages/plotext/_monitor.py:668, in <listcomp>(.0)
    665 axes_ansi = ut.all_ansi(*axes_colors)
    667 # Apply Axes Scale (linear or log)
--> 668 xscale = [self.xscale[0] if self.xside[s] == self.default.xside[0] else self.xscale[1] for s in Signals]
    669 yscale = [self.yscale[0] if self.yside[s] == self.default.yside[0] else self.yscale[1] for s in Signals]
    670 self.x = [self.x[s] if xscale[s] is self.default.xscale[0] else ut.log(self.x[s]) for s in Signals]

IndexError: list index out of range
@Samiisd Samiisd changed the title Crash on second call to plt.show whenver plt.text was used Crash on second call to plt.show whenever plt.text was used May 29, 2022
@Samiisd Samiisd changed the title Crash on second call to plt.show whenever plt.text was used Plotting text crashes on second plot cakk May 29, 2022
@Samiisd Samiisd changed the title Plotting text crashes on second plot cakk Plotting text crashes on second plot call May 29, 2022
@piccolomo
Copy link
Owner

piccolomo commented Jun 12, 2022

Hi @Samiisd,

Cool I will work on this in the following weekends and I will update.

Thanks for posting btw, nicely spotted! Those bug reports are very useful!

Savino

@Samiisd
Copy link
Author

Samiisd commented Jun 16, 2022

Hi @piccolomo, I'm glad that it helps. And thanks for maintaining this lib, great experience so far! 👍

@piccolomo
Copy link
Owner

piccolomo commented Jun 26, 2022

Hi @Samiisd,

I have solved this issue in the new version 5.1, available only on GitHub for now, using:

pip install git+https://github.com/piccolomo/plotext

To install the optional dependencies, instructions are provided here.

Great Issue report btw, I would have not spotted it. The issue was that after the plot, the text data needed to be cleared out, otherwise at each show() it tries to add the same text and plot it, but not properly and so it failed.

Any feedback or double test is appreciated.
Thanks and all the best,
Savino

@piccolomo piccolomo changed the title Plotting text crashes on second plot call Plotting text Crashes on Second show() Call Jun 26, 2022
@piccolomo piccolomo changed the title Plotting text Crashes on Second show() Call Plotting Text Crashes on Second show() Call Jun 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants