0

In matplotlib,I can use plt.yticks to set scale as I liked.

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()

However,when using pyqt5, I have to combile matplotlib with canvas, and it seems it's useless to use 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()

Anybody knows how to use the yticks in pyqt5(maybe in canvas)?

rosefun
  • 1,797
  • 1
  • 21
  • 33

1 Answers1

0

Maybe I know how to solve it by myself.

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