Python Index Error

Hello everybody,

I have only recently started to use NGSolve. I followed the instructions in the Documentation and compiled everything myself.

Now, I seem to have a problem with Python. For example, the Python code test.py

import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.show()

works fine if I call it with Python. As soon as I call it with netgen, I get the error

NETGEN-6.2-dev
Developed by Joachim Schoeberl at
2010-xxxx Vienna University of Technology
2006-2010 RWTH Aachen University
1996-2006 Johannes Kepler University Linz
optfile ./ng.opt does not exist - using default values
togl-version : 2
loading ngsolve library
NGSolve-6.2.2001-62-gaed90e28
Using Lapack
Including sparse direct solver UMFPACK
Running parallel using 4 thread(s)
importing NGSolve-6.2.2001-62-gaed90e28
(should) load python file 'test.py'
Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 2811, in plot
    return gca().plot(
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 935, in gca
    return gcf().gca(**kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 578, in gcf
    return figure()
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 525, in figure
    **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 3218, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/_backend_tk.py", line 1008, in new_figure_manager_given_figure
    window = Tk.Tk(className="matplotlib")
  File "/usr/lib/python3.7/tkinter/__init__.py", line 2018, in __init__
    baseName = os.path.basename(sys.argv[0])
IndexError: list index out of range
Finished executing test.py
Thank you for using NGSolve

For example, when I uncomment the plotting routine in the python example adaptive.py, the same
thing happens. What am I doing wrong?

All the best,
Michael

Anybody?

I think you can just call python as you did when dealing with the matplotlib error above, and then call netgen for visualizing the solution for now.

I’ve had some matplotlib problems as well when using netgen gui. for me it helped to use a different backend in matplotlib, you can either do this by setting environment variables or

import matplotlib
matplotlib.use("WXAGG")

before importing matplotlib.pyplot.
Another option that might help is to start the gui from the python thread, run with python and do

import netgen.gui

at the start of your script. This should open the netgen gui.
Best
Christopher

This error basically means you are trying to access a value at a List index which is out of bounds i.e greater than the last index of the list or less than the least index in the list. So the first element is 0, second is 1, so on. So if there are n elements in a python list, the last element is n-1 . If you try to access the empty or None element by pointing available index of the list, then you will get the "List index out of range " error. To solve this error, you should make sure that you’re not trying to access a non-existent item in a list.

Thank you! The first option worked. Just for reference, if you use Python 3+, you have to install

python3-wxgtk4.0

to get to change the backend.