2

I cannot get matplotlib.pyplot to work under GMS 3.4. It keeps crashing DM with the following error message

This application failed to start because it could not find or load the Qt platform plugin "windows" in "". Reinstalling the application may fix this problem.

When I run this code (taken from the DM Help document):

import sys
sys.argv.extend(['-a', ' '])
import numpy as np
import matplotlib.pyplot as plt

t = np.arange( 0.0, 3.0, 0.01)
s1 = np.sin(2 * np.pi * t)
plt.plot(t, s1)
plt.show()

Reinstalling/repairing GMS doesn't seems to solve the issue. I'm pretty new to Python so I may have missed something obvious?

miile7
  • 2,547
  • 3
  • 23
  • 38
KEVIVI
  • 297
  • 1
  • 8
  • This is unfortunately not a DM-script question, but a Python (configuration) issue. The code above runs just fine on my install of Python and GMS 3.4.0. The MatPlot lib on my system was installed from the anaconda prompt (launched with administrator rights) using `activate GMS_VENV_PYTHON` and then `pip install matplotlib` – BmyGuest Mar 28 '20 at 18:57
  • The versions I'm currenlty using are : Python 3.7.3 (updated), Numpy 1.17.1 (updated) and MatPlotLib 3.1.1 – BmyGuest Mar 28 '20 at 19:04
  • Yes. This is indeed a Python configuration issue. I removed, re-installed GMS 3.4 and installed the matplotlib again and still have no luck (Miniconda3: Python 3.7.2 / Numpy 1.15.4 / MatPlotLib 3.1.1 / qt 5.9.7). I do have another python installation on my computer (Anaconda 3: Python 3.7.3 / Numpy 1.16.2 / MatPlotLib 3.0.3 / qt 5.9.7). Is this the issue? – KEVIVI Mar 29 '20 at 02:07
  • It shouldn't, provided you have not selected that installation as the Python to be used with GMS, but TBH my experience with Python is possibly marginally greater than yours (and spiked with frustrations.) – BmyGuest Mar 29 '20 at 11:29

3 Answers3

2

Even though the fix presented by miile7 does its job, I would like to propose an alternative solution:

As very nicely pointed out by miile7, setting the environment variable globally might interfere with other software and should be avoided. To make DM finding the Qt platform, one can simply

Copy the folder

ProgramData\Miniconda3\envs\GMS_VENV_PYTHON\Library\plugins\platforms

to

Program Files\Gatan

or more specifically to the location of the DigitalMicrograph.exe

A more general, GMS-unrelated thread on this can be found here:

Changing QT_PLUGIN_PATH in environment variables causes programs to fail

Update:

It is probably better to create a junction by executing the following command (with admin rights):

mklink /J "C:\Program Files\Gatan\platforms" "C:\ProgramData\Miniconda3\envs\GMS_VENV_PYTHON\Library\plugins\platforms"

The problem is still fixed this way and in case you update/modify Qt in your Miniconda, DM should still find the latest version.

Community
  • 1
  • 1
1

I wanted to use python in GMS too and I ended up with the same error. This took me several hours to find out why and how to fix it:

TL;DR

Save the following code as start-gatan.bat and use this start-gatan.bat file to start GMS. The file can be placed anywhere. Note that the console window has to stay open while GMS is active!

@echo off

rem
rem File: start-gatan.bat
rem ---------------------

SET QT_PLUGIN_PATH=%ProgramData%\Miniconda3\envs\GMS_VENV_PYTHON\Library\plugins
SET gms_path=%PROGRAMFILES%\Gatan\DigitalMicrograph.exe

echo Starting GMS
echo Do not close this window!

"%gms_path%"

Important note: To show the matplotlib window you have to untick the "Execute on Background Thread"-Checkbox in the footer of the code panel! Also it takes comparatively long to execute.


Cause

This is neither a GMS error nor a python error but a Qt error. For rendering windows matplotlib (by default) uses PyQT which uses Qt. The error tells about the qwindows.dll and that it cannot be found (explicitly?).

Side note: The renderer can be changed by adding e.g. matplotlib.use('GTK3Agg'). This does not show up the Qt-Error anymore but shows another error so I didn't follow this idea.

Solution

The qwindows.dll is located in %ProgramData%/Miniconda3/envs/GMS_VENV_PYTHON/Library/plugins/platforms. The path for the platform plugins can be set in the qt.conf (but this didn't fix it for me which, in this thread they mention that there may be differences) or in the environment variable %QT_PLUGIN_PATH% as I found here.

So add (create) the system variable %QT_PLUGIN_PATH% with the value %ProgramData%\Miniconda3\envs\GMS_VENV_PYTHON\Library\plugins. A system variable in Windows can be added under Control Panel > System > Advanced System Settings > Environment Variables.1

Note that this environment variable should only be present for GMS, not for all other QT programs. Therefore create a start-gatan.bat and copy the code postend at the top in the TL;DR section. Now start GMS by double clicking the start-gatan.bat and your example code will work.

Important note: To show the matplotlib window you have to untick the "Execute on Background Thread"-Checkbox in the footer of the code panel, otherwise the plot window will not be visible.

1Adding a new environment variable works but if you have one other program that uses QT (which is very likely the cause) you will run into the (nearly) same error message but in the other program. If you only have GMS pyqt using QT this may be your solution.

Further notes

Several forum threads including the QT forum tell about duplicate paths for this qwindows.dll. If my answer didn't help you you might want to look into that. Note that checking environment variables has to be done in the miniconda environment. So make sure to run

activate GMS_VENV_PYTHON

before outputting variables. Also you might want to have a look into the qt config which can be found at %PROGRAMDATA%/Miniconda3/envs/GMS_VENV_PYTHON/bin. Note that there always is the file in the normal Miniconda diretory (%PROGRAMDATA%/Miniconda3/bin) and the one in the envs.

miile7
  • 2,547
  • 3
  • 23
  • 38
  • This is very valuable information and I didn't know a lot of it. However, the Matplotlib issue could also be solved in a different (simpler?) way. I will post my out-of-the box solution in a moment. The unforseen problem with that was, that while it worked perfectly for the GMS 3.4.0 release, it unfortunately *didn't* work for the first (free analytical) GMS 3.4.1 beta release. However, Gatan has since updated their (still free analytical) installer on their Webpage. That version is ok again. – BmyGuest Apr 27 '20 at 17:08
  • I didn't find an easier way. I run into that problem with GMS Version 3.2.40.2804.0. – miile7 Apr 28 '20 at 07:09
  • That GMS version does not exist. I guess you've meant 3.4.0.2804? For that version, the instructions in my answer below work for me as well. (Just pip installing matplotlib, essentially). How did you install matplotlib? (conda?) Also a note on 3.4.0 vs 3.4.1: The 3.4.0 version uses MKL libraries for Numpy, whereas 3.4.1 uses OpenBLAS. This is to avoid a DLL version clash with GMS which also uses MKL libraries but of different version. – BmyGuest Apr 29 '20 at 09:06
  • Hmm. I will check my version when I'm back on that computer. I installed matplotlib via `conda install`. I read that one should not use `pip` when using an anaconda distribution but that is some time ago. I thought this information is still up to date. – miile7 Apr 29 '20 at 12:22
  • Possibly still true from the viewpoint of Anaconda, but not from the viewpoint of GMS. I've found `pip` to work more reliable in this scenario, but I guess it depends on a case-to-case sitatuation. I'm really glad you and Pommesgabel posted an alternative way to fix things up. – BmyGuest Apr 29 '20 at 13:53
  • @BmyGuest So I've checked my version and it is 3.40.2804.0. That is what the `Help > About Digital Micrograph`-Window tells me. (Even though I wonder why it is 3.40 and not 3.4.0). I also wanted to update to 3.4.1 but there is no download on https://gatan.sharefile.com/d-sa9b5f88294f46ae9. Also you mention that it is a beta so I guess I can't get this beta from the normal file repository. – miile7 Apr 30 '20 at 07:20
  • 1
    Yes, that sounds correct. (You've had 3.2.40 in your comment above). The 3.4.1 "free" is a special offer due to Covid-19 and directly linked at the main page of the Gatan homepage. Or go [here](https://www.gatan.com/complimentary-gms-3-software-during-covid-19-lab-closures) – BmyGuest Apr 30 '20 at 08:22
0

Tested method to get MatplotLib to run in GMS
(Tested with GMS 3.4.1.2938 beta)

  • Start with clean system (no GMS installed, no Python-for-GMS Miniconda installed)
  • Install GMS with the installer and the Python option enabled.
  • Start the Anaconda-prompt with administrator rights (right-click item in start-menu)
    Start the Anaconda-prompt with administrator rights
  • Activate the GMS_VENV_PYTHON environment by typing activate GMS_VENV_PYTHON
  • Install Matplotlib with the pip installer by typing pip install Matplotlib (requires internet connection)
  • Start DigitalMicrograph and run the following test-script:
# This is a PYTHON script to be run in DigitalMicrograph
if ( False == DM.IsScriptOnMainThread() ):
    print( ' MatplotLib scripts require to be run on the main thread.' )
    exit()

import sys
sys.argv.extend(['-a', ' '])
import matplotlib.pyplot as plt
import numpy as np
a = np.linspace(0, 10, 100)
b = np.exp(-a)
plt.plot(a, b)

DM.ClearResults()
print('Start plotting. Script continues when figure is closed')
plt.show()
print('Done plotting.')

Result

And as a bonus, because this is about DM-scripting: Using the capability of hybrid-scripting one can wrap the Python call in a string and execute it directly from within a DM-script:

// This is a DM-script
string pyScript
pyScript += "import sys" + "\n"
pyScript += "sys.argv.extend(['-a', ' '])" + "\n"
pyScript += "import matplotlib.pyplot as plt" + "\n"
pyScript += "import numpy as np" + "\n"
pyScript += "a = np.linspace(0, 10, 100)" + "\n"
pyScript += "b = np.exp(-a)" + "\n"
pyScript += "plt.plot(a, b)" + "\n"
pyScript += "plt.show()" + "\n"

ClearResults()
Result( "Script continues when figure is closed\n" )
ExecutePythonScriptString( pyScript, 1 ) // On Main-thread
Result( "Done plotting.\n" )
BmyGuest
  • 6,331
  • 1
  • 21
  • 35
  • So you are suggesting to install GMS without the python plugin and then activate the `GMS_VENV_PYTHON` manually from the normal anaconda/python installation? That may be a very good idea. Or am I getting your idea wrong? – miile7 Apr 28 '20 at 07:10
  • 1
    @miile7 Ah! I missed out the install-point in the instructions. Thanks for pointing that out to me. No, my suggestion is just to do a regular (clean) install and then pip-install matplotlib from the environment. This sets things up correctly. (With the newest GMS 3.4.1 installer. The first one from March, however, was broken and *did not* work like that.) – BmyGuest Apr 28 '20 at 07:50