'Matplotlib Qt5Agg backend not found
I have written a Python 3.6 program that reads a .txt file of UTM coordinates, sorts them into counterclockwise order, displays the coordinates on a graph using matplotlib, and then writes the coordinates to a .txt file on the desktop. It works fine when I run it in spyder, which is the IDE that I have been using, but when I convert it to an exe using cx_Freeze (by building my python file) and try to run it, I get the following error: ModuleNotFoundError: No module named 'matplotlib.backends.backend_qt5agg' I have tried installing the Qt5 backend by doing: pip install PyQt5 As well as updating cx_Freeze. Any help would be much appreciated. I am still a beginner with Python as well as programming in general so I apologize if I was not clear enough in my explantion. Below is my code for my main Python script
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
import math as math
import tkinter as tk
from tkinter.filedialog import askopenfilename
import time
def findFilename():
root = tk.Tk()
#root.withdraw()
filename = askopenfilename()
root.destroy()
return(filename)
def findSize(cnt, filename):
#number of lines in file
with open(filename) as f:
for line in f:
cnt = cnt+1;
return cnt;
def findChar(filename):
file = open(filename, "r")
char = ""
lines = file.read()
if(lines.find(" ") == -1):
char = ","
else:
char = " "
return(char)
def inputArr(cnt, arrX, arrY, arrZ, filename, char):
file = open(filename, "r")
num1 = 0
num2 = 0
num3 = 0
place1 = 0
place2 = 0
place3 = 0
for i in range(0, cnt):
num = file.readline()
place1 = num.find(char)
num1 = num[:place1]
num = num[place1 + 1:]
place2 = num.find(char)
num2 = num[:place2]
num = num[place2+1:]
place3 = num.find(char)
num3 = num[:place3]
arrX.append(float(num1))
arrY.append(float(num2))
arrZ.append(float(num3))
return(arrX, arrY, arrZ)
def drawPath(arrX, arrY, label, cnt):
plt.plot(arrX, arrY, '-ok', color = 'red')
plt.xlabel("X coordinates")
plt.ylabel("Y coordinates")
plt.title("Loop path")
plt.show()
for i in range(0,cnt):
label.append(str(i))
for i, txt in enumerate(label):
plt.annotate(txt, (arrX[i], arrY[i]))
def findCenter(arrX, arrY, cnt):
xCenter = 0
yCenter = 0
for i in range(0,cnt):
xCenter += arrX[i]
yCenter += arrY[i]
xCenter /= cnt
yCenter /= cnt
return(xCenter, yCenter)
def moveToCenter(arrX, arrY, arrX1, arrY1, xCenter, yCenter, cnt):
for i in range(0,cnt):
arrX1.append(arrX[i] - xCenter)
arrY1.append(arrY[i] - yCenter)
return(arrX1, arrY1)
def calculateTheta(arrX1, arrY1, arrTheta, cnt):
for i in range(0,cnt):
arrTheta.append(math.atan2(arrY1[i], arrX1[i]))
# print(arrTheta[0])
return(arrTheta)
def sortPoints(arrTheta, arrX, arrY, arrZ, cnt):
minimum = 0
for i in range(0,cnt-1):
minimum = i
for j in range(i + 1, cnt):
if(arrTheta[j] < arrTheta[minimum]):
minimum = j
arrTheta[minimum], arrTheta[i] = arrTheta[i], arrTheta[minimum]
arrX[minimum], arrX[i] = arrX[i], arrX[minimum]
arrY[minimum], arrY[i] = arrY[i], arrY[minimum]
arrZ[minimum], arrZ[i] = arrZ[i], arrZ[minimum]
def writeFile(arrX, arrY, arrZ, cnt, char):
moment = time.strftime("%Y-%b-%d__%H_%M_%S",time.localtime())
file = open("C:\\Users\\natha\\Desktop\\sorted" + str(moment) + ".txt", "w")
num = ""
for i in range(0,cnt):
if(i < 10):
num = "0"
else:
num = ""
file.write("<" + "L" + num + str(i) + ">" + " " + str(arrX[i]) + char +
str(arrY[i]) + char + str(arrZ[i]) + char + "\n")
def main():
cnt = 0
arrX = []
arrY = []
arrZ = []
label = []
arrX1 = []
arrY1 = []
arrTheta = []
xCenter = 0
yCenter = 0
char = ""
filename = findFilename()
char = findChar(filename)
cnt = findSize(cnt, filename)
findChar(filename)
inputArr(cnt, arrX, arrY, arrZ, filename, char)
xCenter, yCenter = findCenter(arrX, arrY, cnt)
arrX1, arrY1 = moveToCenter(arrX, arrY, arrX1, arrY1, xCenter, yCenter, cnt)
arrTheta = calculateTheta(arrX1, arrY1, arrTheta, cnt)
#arrX, arrY, arrZ = randomPoints(arrX, arrY, arrZ, cnt)
sortPoints(arrTheta, arrX, arrY, arrZ, cnt)
writeFile(arrX, arrY, arrZ, cnt, char)
drawPath(arrX, arrY, label, cnt)
main()
As well as my setup.py file
from cx_Freeze import setup, Executable
import sys
import os.path
os.environ['TCL_LIBRARY'] = r'C:\Users\natha\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\natha\Anaconda3\tcl\tk8.6'
additional_mods = ['numpy.core._methods', 'numpy.lib.format']
setup(name='loopProgram',
version='0.4',
description='xyz script',
options = {'build_exe': {'includes': additional_mods}},
executables = [Executable('loopProgram.py')]
)
Solution 1:[1]
For OpenSUSE I found that the above answers didn't work. I tried all of the various backends, and nothing show'd. I was getting no error output from the script running and furthermore the plot.show() function wasn't even blocking the console. plot.savefig()
worked without issue when used properly, as outlined in this thread
This link showed that I needed to add tk-devel
before I installed matplotlib
. Using Zypper I installed tk-devel
, uninstalled python3-matplotlib
, reinstalled matplotlib, and that worked for me.
Solution 2:[2]
Install missing matplotlib qt5 backend sudo apt-get install python-matplotlib-qt5
Get the Matplotlib config file path: python import matplotlib matplotlib.matplotlib_fname() u'/usr/lib64/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc' In the config file, change the backend to qt5agg vi /usr/lib64/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
Change the line to :
backend : qt5agg
Solution 3:[3]
Have you tried to add the following line
import matplotlib.backends.backend_qt5agg
in your main Python script (or in any other appropriate module used in your script) to "force" cx_Freeze to include this module?
Solution 4:[4]
I know this is an old thread but I faced this Problem with Spyder 5 lately. I am using OpenSUSE and I solved this problem just by installing python38-matplotlib-qt5 with
zypper in python38-matplotlib-qt5
Check your python version first though !!! this will probably only work with python3.8 and above.
Solution 5:[5]
Same issue occurred to me also, try reinstalling matplolib
pip3 uninstall matplotlib
then
pip3 install matplotlib
which solved my issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | rajan |
Solution 2 | Neeraj Nair |
Solution 3 | jpeg |
Solution 4 | Ziadhs |
Solution 5 | abhilash |