Author Topic: Matplotlib crashes with OVITO  (Read 161 times)

yketa

  • Newbie
  • *
  • Posts: 3
Matplotlib crashes with OVITO
« on: December 12, 2018, 01:52:34 AM »
Hi all,

I am trying to include some OVITO's functionalities to my code routine in Python.

Things work well unless I try to plot something with matplotlib. This minimal example
Code: [Select]
import ovito
import matplotlib.pyplot as plt
plt.subplots()
returns the error
Code: [Select]
QWidget: Cannot create a QWidget without QApplication
Aborted (core dumped)

What is happening? How can I overcome this?

Thanks!



I use Python 3.6.7, matplotlib 2.1.2 and OVITO 2.9.0, on 64-bit Linux.

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 577
Re: Matplotlib crashes with OVITO
« Reply #1 on: December 12, 2018, 08:57:30 AM »
Hi yketa,

how did you execute this script? Using the 'ovitos' interpreter or the standard Python interpreter on your machine?

-Alex

yketa

  • Newbie
  • *
  • Posts: 3
Re: Matplotlib crashes with OVITO
« Reply #2 on: December 12, 2018, 11:13:30 AM »
Hi Alexander, thanks for your quick reply!

I executed this script with the standard Python interpreter on my machine.

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 577
Re: Matplotlib crashes with OVITO
« Reply #3 on: December 12, 2018, 12:03:57 PM »
I'm not sure if this will suffice to work around the problem, but you can try the following. Both Ovito and matplotlib use the Qt libraries for their user interface and graphical output. This can leads to certain conflicts, because both initialize the Qt system in different ways and results may depend on the order in which you import an initialize the ovito module and the matplotlib module.

Please add the following code line to the very top of your Python source file:

Code: [Select]
from PyQt5.QtWidgets import QApplication
app = QApplication([])

This will initialize the Qt system before anything else, in a way that should be compatible with matplotlib. When you do "import ovito" later, Ovito should detect that it is already initialized and not do it again.

I couldn't test this workaround myself. I don't have access to a Linux machine at the moment. Let me know what happens.

In any case, it should be possible to run matplotlib with a non-interactive backend to avoid the Qt conflict. A non-interactive backend doesn't display any window interface and only outputs the plot to a file. For example:
Code: [Select]
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Agg')

yketa

  • Newbie
  • *
  • Posts: 3
Re: Matplotlib crashes with OVITO
« Reply #4 on: December 12, 2018, 04:36:02 PM »
Initialising the Qt system indeed avoids the error (even after importing OVITO), so everything works fine now, thank you very much!

However, running matplotlib with a non-interactive backend is a solution I had tried and which did not work.