From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Python node returns 'list index out of range' when using the popular matplotlib python library

I'm using Python 3.7 and LabVIEW 18.0f2. I have a simple python script that generates a graph of some data and pops up the plot in a new window using the matplotlib library. When I call this from the python node in LabVIEW, it throws an error because the matplotlib library is trying to use 'sys.argv[0]', which contains the python script file name when run normally, but is empty when called from LabVIEW. 

 

I can get around this by setting the value manually in my python script (sys.argv = ['graph.py']), but I'm wondering if there is a better way to handle this?

 

graph.png

0 Kudos
Message 1 of 6
(3,012 Views)

I'm no Python expert, but this article https://www.pythonforbeginners.com/system/python-sys-argv says that the sys.argv variable is the list of variables passed to the command-line argument. I don't think LabVIEW runs Python scripts via a command line argument; it seems more like you're calling it directly within a Python session, so it makes sense that the sys.argv variable would be empty.

 

I don't know what the Windows command line syntax is for this, but if you wanted to run it as a command line argument I think you could use System Exec.vi to call a Python.exe command.

Message 2 of 6
(2,961 Views)

Calling from the system exec node was my backup plan, but then I got it working by setting the value of sys.argv manually. It just seemed a bit dirty. I was hoping for some context on what the python node is doing behind the scenes. I think you're right, though. It acts more like a python session where the initialization code gets run (imports, etc.), and then each function can be called on demand.

0 Kudos
Message 3 of 6
(2,937 Views)

Not that this solves your problems with calling python in any way, but you can use a lot of Matplotlib functionality directly in LabVIEW by using the Advanced Plotting Toolkit - http://sine.ni.com/nips/cds/view/p/lang/en/nid/213166

 

It doesn't support everything, but quite useful for making nice plots.

Craig

Message 4 of 6
(2,754 Views)

hi wondering if you can help. I have a main python script that imports two other scripts. One of the imported scripts simply contains functions, is called S2Functions.py and they all contain matplotlib methods.

I have tried to put 

plt.sys.argv =['S2Functions.py'] after i imported matplotlib but this doesn't seem to solve the problem, I also tried it in the main python script but to no avail. Any ideas?
Obvs im calling the main python file from labview
 
0 Kudos
Message 5 of 6
(1,713 Views)

You can take a look at how I did it in my script attached to the original post. I had to manually set sys.argv, then create the plot. You didn't share your script, so maybe it's a typo, but you put "plt.sys.argv". Try removing the plt.

 

Also, make sure you "import sys" first. You may have to add the names of all of your python scripts to the sys.argv list?

0 Kudos
Message 6 of 6
(1,685 Views)