1

I am trying to test my matplotlib by a simple code like:

from matplotlib import pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()

but when I run this code in Terminal by Python3 test.py nothing happens. Terminal freezes and I need to press Ctrl+z.

I used sudo apt-get install python3-matplotlib to install the library, and it installed successfully.

Append:

Thank you people for your help. Actually none of the responses could help so, I decided to use Spyder instead. And it worked great there. Although I couldn't find the source of issue, but I am happy that my code works in Spyder and I can continue my work.

andrew.46
  • 39,359
Rooby84
  • 11

2 Answers2

0

Here is the output that I got from running your code in Spyder IDE which can be installed with sudo apt install spyder in Ubuntu 24.04. This shows that there's nothing wrong with your Python code.

enter image description here

In my Ubuntu 24.04 the ipython3 command starts an enhanced interactive Python 3 shell which produces similar output, not surprisingly because Spyder has the python3-ipython package as a dependency.

When you try to run run the same code in the terminal nothing happens, whether you try to run it from the python3 prompt or from the ipython3 prompt. From this I conclude that there's something wrong with your terminal. Maybe it wouldn't freeze if you ran the same code in xterm (sudo apt install xterm). It's worth a try.

karel
  • 122,292
  • 133
  • 301
  • 332
0

The same happens for me. The reason might be ipython in terminal can't find graphical widget anymore.

One of the solution is to use plotly, which can open the figure in browser anyway.

pip install plotly

plotly is at least comparable to matplotlib, it's also pre-installed in Google colab.