【问题标题】:Python GUI Plot axis and title labelsPython GUI 绘图轴和标题标签
【发布时间】:2021-06-01 15:15:46
【问题描述】:

我在标记轴和给我的绘图命名时遇到问题。我正在使用 QT Designer 创建一个 ui 文件。有人可以帮忙吗?

class MplCanvas(FigureCanvas):

    def __init__(self, parent=None, width=6, height=5, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)


    x_pos = np.arange(len(All_Runs_Names))
    # plot figure
    sc = MplCanvas(self, width=5, height=4, dpi=100)

    # assign data here
    sc.axes.bar(x_pos, All_Runs_Total_Errors, color = (0.5,0.1,0.5,0.6))
    
    # toolbar and layout creation
    toolbar = NavigationToolbar(sc, self.MyWindow)
    layout = QtWidgets.QVBoxLayout()
    layout.addWidget(toolbar)
    layout.addWidget(sc)
    self.MyWindow.plotWidget.setLayout(layout)

【问题讨论】:

    标签: python user-interface bar-chart axis-labels


    【解决方案1】:

    由于您使用的是add_subplot,请查看this 以了解如何为子图设置标题和标签。如果看不到剧情标题,可能需要adjust_subplots

    另外,考虑使用 QtAgg 后端(版本取决于您使用的 pyqt 版本),原因在 here 中描述。

    干杯。

    PS。 为了我自己的目的,我做了类似的事情,虽然使用add_axes而不是add_subplot

    import matplotlib
    matplotlib.use('Qt5Agg')
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
    from matplotlib.figure import Figure
    
    from PyQt5 import QtWidgets, uic
    import sys
    
    class Window(QtWidgets.QMainWindow):
        def __init__(self):
            super(Window, self).__init__()
            uic.loadUi('path_to_file\\file.ui', self)
            
            self.my_chart = MplCanvas(self)
            layout = self.mychart_layout
            layout.addWidget(self.my_chart)
    
            self.something_to_do([1,2,3,4],[1,2,3,4])
    
            self.show()
    
        def something_to_do(self, xdata, ydata):
            self.my_chart.axes.set_title("title")
            self.my_chart.axes.set_position([0.07,0.1,0.87,0.8]) # here is repositioning of the axes
            # if the default (in the MplCanvas class) cannot reveal the title set
            # definitely something you should tweak.
            line, = self.my_chart.axes.plot(xdata, ydata) # line handle for updating the line during runtime
    
    
    class MplCanvas(FigureCanvasQTAgg):
    
        def __init__(self, parent=None, width=5, height=4, dpi=100):
            fig = Figure(figsize=(width, height), dpi=dpi)
            self.axes = fig.add_axes([0.07,0.165,0.925,0.83]) # initial rectangle for any canvas' axes ([left, bottom, width, height])
            self.axes.set_ylabel('y label')
            self.axes.set_xlabel('x label')
            super(MplCanvas, self).__init__(fig)
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        window = Window()
        app.exec_()
    

    【讨论】:

      猜你喜欢
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      相关资源
      最近更新 更多