gpt4 book ai didi

python - Matplotlib Qt5Agg 后端未找到

转载 作者:行者123 更新时间:2023-11-30 22:11:21 24 4
gpt4 key购买 nike

我编写了一个 Python 3.6 程序,它读取 UTM 坐标的 .txt 文件,将它们按逆时针顺序排序,使用 matplotlib 在图形上显示坐标,然后将坐标写入桌面上的 .txt 文件。当我在spyder(我一直在使用的IDE)中运行它时它工作正常,但是当我使用cx_Freeze(通过构建我的python文件)将其转换为exe并尝试运行它时,我收到以下错误: ModuleNotFoundError:没有名为“matplotlib.backends.backend_qt5agg”的模块我尝试通过以下方式安装 Qt5 后端: pip 安装 PyQt5以及更新 cx_Freeze。任何帮助将非常感激。我仍然是 Python 以及一般编程的初学者,所以如果我的解释不够清楚,我深表歉意。下面是我的主要 Python 脚本的代码

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()

以及我的 setup.py 文件

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')]
)

最佳答案

安装缺少的 matplotlib qt5 后端 sudo apt-get install python-matplotlib-qt5

获取 Matplotlib 配置文件路径: Python 导入 matplotlib matplotlib.matplotlib_fname() u'/usr/lib64/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc'在配置文件中,将后端更改为qt5agg vi/usr/lib64/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc

将行更改为:

后端:qt5agg

关于python - Matplotlib Qt5Agg 后端未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51444413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com