Fail to plot in ArcGIS Pro Python

1345
4
12-25-2020 02:30 AM
sarah-paradis
New Contributor

When trying to plot using Seaborn module in the ArcGIS Pro (2.6) Python shell, I get an error from the tkinter package:

File "C:\Users\Sarah\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\tkinter\__init__.py", line 2018, in __init__
baseName = os.path.basename(sys.argv[0])
IndexError: list index out of range

The minimum code that reproduces the error is as follows:

import tkinter

tkinter._test()

I tried updating the packages, but it doesn't solve the problem. When executing the same two lines of code outside ArcGIS Pro (directly from the python shell), there is no error. Hence, I assume it is an intrinsic ArcGIS Pro error.

4 Replies
DanPatterson
MVP Esteemed Contributor

You are better off using Seaborn outside of the Pro's python shell.

I use Spyder Spyder 4 ... the python IDE for science - GeoNet, The Esri Community

It has many graphic options from inline to separate floating windows.

If your intent is to plot maps, then use the notebook within pro


... sort of retired...
sarah-paradis
New Contributor

Yes, that works, but I think it should be possible within ArcGIS Pro's Python shell.  

0 Kudos
DanPatterson
MVP Esteemed Contributor

I don't know if the shell is tied to the base environment or it will use the cloned environment.

I suspect the python shell is somewhat limited in its capabilities and is intended for quick runs, or using Pro's graphic capabilities and not those of external modules.Since qt console is installed with jupyter, you can use it as a quick shell.  I just create a shortcut on the desktop.

Target
C:\... install folder ...\bin\Python\envs\arcgispro-py3\pythonw.exe "C:\... install folder ...\bin\Python\envs\arcgispro-py3\Scripts\jupyter-qtconsole-script.py"

Start in
C:\... install folder ...\bin\Python\envs\arcgispro-py3

Although I tend to use Spyder or Notebook for most work


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

@sarah-paradis  another option to the ones mentioned above would be to change the matplotlib backend.

Here, the issue is tkinter is not given a GUI window by ArcGIS Pro and it is receiving an empty list that it is trying to invoke GUI elements into. If you are okay with writing your matplotlib figures to local files, as opposed to pop-up figures, you can change the backend of the Python instance via:

 

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2,2, figsize=(15, 6))
plt.savefig('C:\my_folder\figure.png')

Here subplots is just an example, you can call any figure you would like from here on out.

 

Here it is important to import matplotlib first and setting the backend to agg, before calling matplotlib.pyplot. If you import matplotlib.pyplot, it will set the backend to TkAgg and will not let you change the backend to agg. Once this is done, your matplotlib calls will not require pop-ups windows, that ArcGIS Pro will not allow, and you can create figures and write them to local files.