简体   繁体   English

Matplotlib 未找到 Qt5Agg 后端

[英]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.我编写了一个 Python 3.6 程序,它读取 UTM 坐标的.txt 文件,将它们按逆时针顺序排序,使用 matplotlib 在图形上显示坐标,然后将坐标写入桌面上的.txt 文件。 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.当我在 spyder 中运行它时它工作正常,这是我一直在使用的 IDE,但是当我使用 cx_Freeze 将它转换为 exe(通过构建我的 python 文件)并尝试运行它时,我收到以下错误:ModuleNotFoundError :没有名为“matplotlib.backends.backend_qt5agg”的模块 我尝试通过以下方式安装 Qt5 后端:pip 安装 PyQt5 以及更新 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.我仍然是 Python 以及一般编程的初学者,所以如果我的解释不够清楚,我深表歉意。 Below is my code for my main Python script下面是我的主要 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()

As well as my setup.py file以及我的 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')]
    )

For OpenSUSE I found that the above answers didn't work.对于 OpenSUSE,我发现上述答案不起作用。 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.show() 函数甚至没有阻塞控制台。 plot.savefig() worked without issue when used properly, as outlined in this thread plot.savefig()在正确使用时可以正常工作,如该线程中所述

This link showed that I needed to add tk-devel before I installed matplotlib . 此链接显示我需要在安装matplotlib之前添加tk-devel Using Zypper I installed tk-devel , uninstalled python3-matplotlib , reinstalled matplotlib, and that worked for me.使用 Zypper 我安装了tk-devel ,卸载了python3-matplotlib ,重新安装了 matplotlib,这对我有用。

Install missing matplotlib qt5 backend sudo apt-get install python-matplotlib-qt5安装缺少的 matplotlib qt5 后端 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获取 Matplotlib 配置文件路径: python import 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

Change the line to :将行更改为:

backend : qt5agg后端:qt5agg

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?在您的主要 Python 脚本(或您的脚本中使用的任何其他适当模块)中“强制” cx_Freeze 包含此模块?

I know this is an old thread but I faced this Problem with Spyder 5 lately.我知道这是一个旧线程,但我最近在使用 Spyder 5 时遇到了这个问题。 I am using OpenSUSE and I solved this problem just by installing python38-matplotlib-qt5 with我正在使用 OpenSUSE,我通过安装 python38-matplotlib-qt5 解决了这个问题

zypper in python38-matplotlib-qt5

Check your python version first though !!!不过先检查你的python版本!!! this will probably only work with python3.8 and above.这可能只适用于 python3.8 及更高版本。

Same issue occurred to me also, try reinstalling matplolib我也遇到同样的问题,尝试重新安装 matplolib

pip3 uninstall matplotlib

then然后

pip3 install matplotlib

which solved my issue.这解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有Qt5Agg后端的matplotlib返回空的ticklabels - matplotlib with Qt5Agg backend returns empty ticklabels 无法使用 Qt5Agg 后端渲染 matplotlib 图 - Can't render a matplotlib graph using Qt5Agg backend 在4K屏幕上使用Matplotlib和TKAgg或Qt5Agg后端 - Using Matplotlib with TKAgg or Qt5Agg backend on 4K screen 调试:matplotlib.pyplot:加载后端 qt5agg 版本未知 - DEBUG:matplotlib.pyplot:Loaded backend qt5agg version unknown Matplotlib Funcanimation 在 Qt5Agg 后端调整 window 大小时恢复循环 - Matplotlib Funcanimation resumes loop upon window resizing in Qt5Agg backend VSCode 无法切换 matplotlib 后端:ImportError: Cannot load backend 'Qt5Agg' which requires the 'qt5' interactive framework - VSCode cannot switch matplotlib backend: ImportError: Cannot load backend 'Qt5Agg' which requires the 'qt5' interactive framework 无法加载需要“qt5”交互框架的后端“Qt5Agg”,因为“headless”当前正在运行 - Cannot load backend 'Qt5Agg' which requires the 'qt5' interactive framework, as 'headless' is currently running Matplotlib:自定义Qt4Agg后端 - Matplotlib: Customizing Qt4Agg Backend PyCharm和ipython的组合无法导入qt5或Qt5Agg - Combination of PyCharm and ipython fails to import qt5 or Qt5Agg 使用Qt4Agg后端在Matplotlib中重置工具栏历史记录 - Reset Toolbar History in Matplotlib with Qt4Agg backend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM