【问题标题】:How could show x and y labels on PyQt canvas?如何在 PyQt 画布上显示 x 和 y 标签?
【发布时间】:2020-04-20 23:13:36
【问题描述】:

我尝试使用 Python 和 PyQt 制作桌面应用程序。 我想做的很简单,从 txt 文件加载阻抗数据,然后绘制它。

我在对话框中创建了canvas 并在其上绘制了线条,但没有显示xlabelylabel

GUI 上的标签问题

当我在没有 GUI 的情况下运行脚本时,它可以很好地显示标签。

没有图形界面

似乎轴的大小太大而无法在布局上显示标签。我能控制它吗?

import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

from PyQt5.QtWidgets import *
from PyQt5 import uic

uiFile = "../UI/DataAnalyzer.ui"

class MainDialog(QDialog):
    def __init__(self):
        QDialog.__init__(self, None)
        uic.loadUi(uiFile, self)

        self.fileDialog_btn_1.clicked.connect(self.loadFile)

        self.fig = plt.Figure()
        self.ax = self.fig.add_subplot(111)

        self.canvas = FigureCanvas(self.fig)
        self.lay.addWidget(self.canvas)
        self.canvas.draw()    

    def loadFile(self):
        fileNames = QFileDialog.getOpenFileNames(self)
        self.Z_processing(fileNames)

    def Z_processing(self, fileNames):
        # print(fileNames[0])
        # print(type(fileNames[0]))

        self.ax.cla()

        for file in fileNames[0]:
            data = np.loadtxt(file)
            self.ax.plot(data[:, 0], data[:, 1])
            self.ax.plot(data[:, 0], data[:, 2])

        self.ax.grid()
        self.ax.set_xlim([0, 20e6])
        self.canvas.draw()

app = QApplication(sys.argv)
mainDialog = MainDialog()
mainDialog.show()
app.exec_()

【问题讨论】:

    标签: python matplotlib pyqt


    【解决方案1】:

    尝试在Z_processing() 中绘制画布之前调用self.fig.tight_layout()

    【讨论】:

    • 感谢您的回复。我已经这样做了,但它不起作用。当我将 add_axes() 与几何输入一起使用时,或者当我增加布局的大小时,它会显示更好的布局。
    猜你喜欢
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2018-04-05
    相关资源
    最近更新 更多