在python中使用pyqt5旋转或调整轴刻度

时间:2018-04-30 09:37:36

标签: python python-3.x matplotlib pyqt5

我使用以下代码

    import sys
    from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout, \
    QLineEdit, QMessageBox, QInputDialog, QLabel, QHBoxLayout, QGridLayout,     QStackedLayout, QFormLayout
    from  PyQt5 import QtCore, QtGui, QtWidgets
    import time
    import matplotlib
    matplotlib.use("TkAgg")
    from matplotlib.lines import Line2D

    import matplotlib.animation as animation
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as     NavigationToolbar
    import matplotlib.pyplot as plt
    from matplotlib.figure import Figure

    # data
    import numpy as np
    #import matplotlib.pyplot as plt
    import pandas as pd
    import os
    import csv




    # percent  MW
   x=['BE2000',
 'BE2020',
 'BE5000',
 'BE6010',
 'BE6017',
 'BE6020',
 'BE6027',
 'BE6030',
 'BE6050',
 'BE6057',
 'BE6061',
 'BE6062',
 'BE6063',
 'BE6070',
 'BE6073',
 'BE6075',
 'BE6080',
 'BE6090',
 'BE7000',
 'BE7010']
y=[0.0002716766988612973,
 0.005178087393490427,
 0.0014053668695097226,
 0.3174139251746979,
 0.006049724003653125,
 0.24824287385322272,
 0.0004986331396716525,
 0.19624266416568525,
 0.13170894569069627,
 0.0028535946936992873,
 0.0002737864422892905,
 0.0011817396106664916,
 0.0029533382584451574,
 0.03281361420815501,
 0.000273411124091493,
 0.002558801432193384,
 0.027004861403488886,
 0.004918720459633545,
 0.006030278629435105,
 0.0002858345295419518]     





    #figure = plt.figure()

    H = np.array([[100, 2, 39, 190], [402, 55, 369, 1023], [300, 700, 8, 412], [170, 530, 330, 1]])
    Z = np.array([[3, 290, 600, 480], [1011, 230, 830, 0], [152, 750, 5, 919], [340, 7, 543, 812]])


    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.im = None
            self.canvas = FigureCanvas(self.figure)

            self.canvas.mpl_connect('button_press_event', self.on_button_press_event)

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

            self.timer = QtCore.QTimer(self)
            self.timer.timeout.connect(self.plot)
            self.timer.setInterval(500)

            # Just some button connected to `plot` method
            self.button = QPushButton('Plot')
            self.button.clicked.connect(self.timer.start)
            self.button.setDefault(False)

            self.stop = QPushButton("Stop")
            self.stop.clicked.connect(self.timer.stop)
            self.stop.setDefault(False)

            self.exit = QPushButton('Exit')
            self.exit.clicked.connect(self.close)
            self.exit.setDefault(True)




            # set the layout
            layout = QFormLayout()
            layout.addWidget(self.toolbar)
            layout.addWidget(self.canvas)
            layout.addWidget(self.button)
            layout.addWidget(self.stop)
            layout.addWidget(self.exit)


            self.setLayout(layout)

            self.lb = QtWidgets.QLabel(self)
            self.lb.setWindowFlags(QtCore.Qt.ToolTip)



        def plot(self):
            data=x
            self.setWindowTitle("Bestandseingruppierung")

            # instead of ax.hold(False)
            self.figure.clear()

            # create an axis
            ax = self.figure.add_subplot(111)
            plt.setp(ax.get_xticklabels(), rotation=45)
            #fig.autofmt_xdate()
            # discards the old graph
            ax.hold(False) # deprecated, see above

            # plot data
            ax.axes.bar(data,y)
            self.canvas.draw()




        def on_button_press_event(self, event):
            print('button={}, x={}, y={}, xdata={}, ydata={}'
                .format(event.button, event.x, event.y, event.xdata, event.ydata))
            if self.im:
                message = str(self.im.get_cursor_data(event))
                delay = 1000
                w = self.lb.fontMetrics().width(message)
                self.lb.resize(w, self.lb.size().height())
                self.lb.setText(message)
                self.lb.move(QtGui.QCursor.pos())
                self.lb.show()
                QtCore.QTimer.singleShot(delay, self.lb.hide)




    if __name__ == '__main__':
        app = QApplication(sys.argv)

        main = Window()
        main.show()

        sys.exit(app.exec_())

生成动态条形图。现在情节中存在问题。 The ticks on the x-axis are fuzzy

我尝试ax.set_xticklabels在两个不同的位置旋转x轴上的刻度线:

1)创建数字后直接:

 class Window(QDialog):
        def __init__(self, parent=None):
            super(Window, self).__init__(parent)
            # a figure instance to plot on
            self.figure = plt.figure()
            self.figure.set_xticklabels(rotation=45)

会产生错误。

2)作为第二次尝试,我把它放在

def plot(self):
        data=x
        self.setWindowTitle("Bestandseingruppierung")
        # instead of ax.hold(False)
        self.figure.clear()

        # create an axis
        ax = self.figure.add_subplot(111)
        ax.set_xticklabels(rotation=45)
        #fig.autofmt_xdate()
        # discards the old graph
        ax.hold(False) # deprecated, see above

        # plot data
        ax.axes.bar(data,y)
        self.canvas.draw()

在这种情况下,我得到旧的情节,没有任何错误,但任何轮换!

我想知道哪些选项以及我应该放在哪里以旋转刻度或使用pyqt5调整刻度或标签的大小!

1 个答案:

答案 0 :(得分:0)

您的第二次尝试非常正确!

只需将您的x标签添加到ax.set_xticklabels(rotation=45)作为第一个位置参数即可。

因此,复制此行代码并将其放入第二次尝试:

ax.set_xticklabels(labels=xlabels, rotation=45)

其中xlabels =您的x轴标签列表