Complete error
Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay
Solution
Because matplotlib is used in the backend, Add the following codes in the drawing code:
import matplotlib
matplotlib.use('Agg')
Then use the following code after the drawing is over:
plt.close()