在PyQt5中使用xtics时python崩溃

时间:2018-07-27 01:21:49

标签: python user-interface matplotlib pyqt5

我正在新闻数据中制作一种意见调查工具, 我想使用PyQt5通过GUI显示此工具。 我做了一些报告,但是在操作GUI时崩溃

通过调试过程,我发现其中存在一些错误 “ xtics”

我该如何解决?

我的数据格式如下:

我的数据:点击此处

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import pandas as pd
class MyWindow(QWidget):
def __init__(self):
    super().__init__()
    self.setupUI()

def setupUI(self):
    self.setGeometry(600, 200, 1200, 600)
    self.setWindowTitle("PyChart Viewer v0.1")
    self.setWindowIcon(QIcon('icon.png'))

    self.lineEdit = QLineEdit()
    self.pushButton = QPushButton("Draw chart")
    self.pushButton.clicked.connect(self.pushButtonClicked)

    self.fig = plt.Figure()
    self.canvas = FigureCanvas(self.fig)

    leftLayout = QVBoxLayout()
    leftLayout.addWidget(self.canvas)

    # Right Layout
    rightLayout = QVBoxLayout()
    rightLayout.addWidget(self.lineEdit)
    rightLayout.addWidget(self.pushButton)
    rightLayout.addStretch(1)

    layout = QHBoxLayout()
    layout.addLayout(leftLayout)
    layout.addLayout(rightLayout)
    layout.setStretchFactor(leftLayout, 1)
    layout.setStretchFactor(rightLayout, 0)

    self.setLayout(layout)

def pushButtonClicked(self):
    data = pd.read_csv("op_test.csv", encoding='utf8', index_col=0)

    r = range(len(data))
    names=list(data)
    positive_bar = data.Positive
    neutral_bar = data.Neutral
    negative_bar = data.Negative
    bar_width = 0.85
    ax=self.fig.add_subplot(111)
    po_bar = ax.bar(r, positive_bar, color="#85c7f9", edgecolor='white', width=bar_width)
    nu_bar = ax.bar(r, neutral_bar, color="#ccf985", bottom=positive_bar, edgecolor='white', width=bar_width)
    ng_bar = ax.bar(r, negative_bar, color="#f99485", bottom=positive_bar + neutral_bar, edgecolor='white',
                     width=bar_width)
    ax.legend([ng_bar, nu_bar, po_bar], ["Negative", "Neutral", "Positive"])
    ax.xtics(r,names) ##Problem here ##
    self.canvas.draw()

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MyWindow()
   window.show()
   app.exec_()

0 个答案:

没有答案