[英]Why isn't my line graph showing up on GUI
This is a function which is being called on a button click. 这是单击按钮时调用的功能。 It is supposed to display a line graph why is it not showing up.
应该显示一个折线图,为什么它不显示。
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()
If you want to draw on PyQt
you must use FigureCanvas
, not matplotlib.pyplot
, so if you use plt.plot()
you will not draw anything. 如果要在
PyQt
上绘图,则必须使用FigureCanvas
,而不要使用matplotlib.pyplot
,因此,如果您使用plt.plot()
,则不会绘制任何内容。
In your case a similar example would be: 在您的情况下,类似的示例是:
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_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.