这是一个按下按钮时调用的功能。它应该显示一个折线图,为什么它没有出现。
def plotk2(self):
#----------------------------------------------------------------Data
temp=full_dataset[['country_txt','iyear','nkill']]
text = self.k_count.currentText()
temp = temp[temp['country_txt'].str.match(text)]
temp2=temp.groupby(['iyear'])['nkill'].count()
temp2=temp2.to_frame()
temp2['iyear']=temp2.index
#----------------------------------------------------------------
self.figure.clear()
ax = self.figure.add_subplot(111)
plt.plot( 'nkill', 'iyear', data=temp2)
#ax.axis('off')
plt.show()
self.canvas.draw()
答案 0 :(得分:2)
如果您想在PyQt
上绘图,则必须使用FigureCanvas
,而不是matplotlib.pyplot
,所以如果您使用plt.plot()
,则不会使用任何内容。
在您的情况下,类似的例子是:
import sys
import pandas as pd
import matplotlib
matplotlib.use('Qt5Agg')
from PyQt5 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
QtWidgets.QWidget.__init__(self, *args, **kwargs)
self.figure = Figure(figsize=(5, 4), dpi=100)
self.canvas = FigureCanvas(self.figure)
self.canvas.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
button = QtWidgets.QPushButton("random plot")
button.clicked.connect(self.plot)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.canvas)
lay.addWidget(button)
self.plot()
def plot(self):
self.figure.clear()
ax = self.figure.add_subplot(111)
d = {'nkill': [1, 2, 4, 5, 6], 'iyear': [3, 4, 5, 5, 5]}
df = pd.DataFrame(data=d)
ax.plot('nkill', 'iyear', data=df)
# ax.axis('off')
self.canvas.draw()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())