pyqt5如何在绘图中使用yticks?

时间:2018-05-06 06:26:12

标签: python matplotlib canvas pyqt pyqt5

在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(可能在画布中)?

1 个答案:

答案 0 :(得分:0)

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

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