首页 > 解决方案 > pyqt5如何在绘图中使用yticks?

问题描述

在 matplotlib 中,我可以使用 plt.yticks 来设置我喜欢的比例。

def plot(self,data):
        ''' plot some random stuff '''

        plt.figure()
        plt.plot(data,'ro')
        plt.yticks([1, 2, 3, 4,5,],['Fault 1', 'Fault 2','Fault 3','Fault 4','Fault 5',])                 
        plt.show()

但是,在使用pyqt5时,我必须将matplotlib与canvas结合起来,使用yticks似乎没用。

class Window(QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # a figure instance to plot on
        self.figure = plt.figure()

        # this is the Canvas Widget that displays the `figure`
        # it takes the `figure` instance as a parameter to __init__
        self.canvas = FigureCanvas(self.figure)

        # this is the Navigation widget
        # it takes the Canvas widget and a parent
        self.toolbar = NavigationToolbar(self.canvas, self)

        # Just some button connected to `plot` method
        self.button = QtWidgets.QPushButton('Plot')
        self.button.clicked.connect(self.plot)

        # set the layout
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        layout.addWidget(self.button)
        self.setLayout(layout)

    def plot(self,data):
        ''' plot some random stuff '''
        # random data
        # data = [random.random() for i in range(10)]

        # create an axis
        ax = self.figure.add_subplot(111)

        # discards the old graph
        ax.hold(False)

        # plot data
        ax.plot(data, '*-')

        ax.yticks([1, 2, 3, 4,5,],['Fault 1', 'Fault 2','Fault 3','Fault 4','Fault 5',]) 
        #ax.set_yticks([1, 2, 3, 4,5,],['Fault 1', 'Fault 2','Fault 3','Fault 4','Fault 5',]) 

        # refresh canvas
        self.canvas.draw()

有人知道如何在 pyqt5 中使用 yticks(也许在画布中)?

标签: pythonmatplotlibcanvaspyqtpyqt5

解决方案


也许我知道如何自己解决它。

list1=list(range(1,6))
ax.set_yticks(list1)
ax.set_yticklabels(['fault'+str(i) for i in list1])

推荐阅读