Home > other >  Using pyqt5 graphical interface display matplotlib figure painting
Using pyqt5 graphical interface display matplotlib figure painting

Time:11-04

To press a button to display a picture, a graphical interface is QT Designer generated, check a lot of information are used his button code generation, and drawing function is inside pyqt5 class, my idea is to use QT Designer generated graphical interface function call statement, for only he put function in the another py files, so the drawing function how to write

CodePudding user response:

Idea: in QT Designer design a layout, in the main program to add matplotlib layout, reference: https://blog.csdn.net/qissme/article/details/109098741

CodePudding user response:

Thank you but the initUI the drawing functions and also in the class mwindow showbox, is there any way I can put the function outside, put in another py files

CodePudding user response:

Found the new problems, drawing can draw, but can only such, the following code
Def the Draw (self) :
AgeList=[' 10 ', '21', '12', '14' and '25']
NameList=[' Tom ', 'Jon', 'Alice' and 'Mike', 'Mary']
Data from the AgeList # converted to type int
AgeList=list (map) (int, AgeList)
# will be x, y axis into matrix
Self. X=np. Arange (len (NameList)) + 1
The self) y=np) array (AgeList)
# tick_label behind with the value of the x axis (optional options: behind the color with the color of the column type, behind the width to the width of the cylinder)
PLT. Bar (range (len (NameList)), AgeList, tick_label=NameList, color='green', width=0.5)
# according to data on the cylinder
For a, b in zip (self. X, the self. The y) :
1 PLT. Text (a, b, b '% d %, ha=' center ', va='bottom')
# set the title
PLT. Title (" Demo ")
# drawing
Self. Canvas. The draw ()

I is to use data frames to drawing, the following code
# set the drawing style
PLT. Style. Use (' ggplot)
# map age nuclear density under different income levels
Income, age [income. Income=='& lt;=50 k]. Plot (kind=' kde ', label='& lt;=50 k ', legend=True, graphics.linestyle='-')
Income, age [income. Income=='& gt; 50 k]. Plot (kind=' kde ', label='& gt; 50 k ', legend=True, graphics.linestyle='-')
PLT. The show ()

But put it keep up with the def the Draw (self) to merge, setting up a button for binding, will card, and then flash back, following
The Process finished with exit code - 1073740791 (0 xc0000409)

I will try to see what the reason, found to be def the Draw (self) I not to read in a data frame, but the code and no error, but also to the statement, normal without graphical interface code below
Income=pd. Read_excel (r 'income. XLSX', encoding='latin-1)
Income. Shape
Have a problem that's not a problem, change to the following
Def the Draw (self) :
Print (income. Shape)
Has been carried out in front of the import data function, click on the button of the function binding, have card, and then flash back, following
The Process finished with exit code - 1073740791 (0 xc0000409)
The class MyMainWindow (QMainWindow Ui_MainWindow) :
Def __init__ (self, parent=None) :
Super (MyMainWindow, self) __init__ (parent)
Self. SetupUi (self)

A few QWidgets #
Self. Figure=PLT. Figure (#) optional parameters, facecolor as the background color in
Self. Canvas=FigureCanvas (self. Figure)
# self. Button_draw=QPushButton (" drawing ")
# connect events
# self. Button_draw. Clicked. Connect (self. The Draw)
Self. PushButton_show. Clicked. Connect (self. The Draw)
Self. PushButton_clear. Clicked. Connect (self. The Clear)
# set layout
# layout=QVBoxLayout ()
The self. The verticalLayout. AddWidget (self. Canvas)
# layout. AddWidget (self. Button_draw)
# self. SetLayout (layout)

Def the Draw (self) :
Print (income. Shape)

This is pyqt5 class

CodePudding user response:

Don't have to speak, to explain the code posted what's the problem, you're too disorderly posted several times this

 
Print (' this post code ')

CodePudding user response:

This code is interesting, logical thinking is rigorous, spent 20 minutes around understand, strongly recommend the use of such expressions of habit,

CodePudding user response:

Def Data_read () :
Global income
Income=pd. Read_excel (r 'income. XLSX', encoding='latin-1)
Print (" data read success!" )
The class MyMainWindow (QMainWindow Ui_MainWindow) :
Def __init__ (self, parent=None) :
Super (MyMainWindow, self) __init__ (parent)
Self. SetupUi (self)
A few QWidgets #
Self. Figure=PLT. Figure (#) optional parameters, facecolor as the background color in
Self. Canvas=FigureCanvas (self. Figure)
# self. Button_draw=QPushButton (" drawing ")
# connect events
Self. PushButton_2. Clicked. Connect (self. The Draw)
# set layout
# layout=QVBoxLayout ()
The self. The verticalLayout. AddWidget (self. Canvas)
# layout. AddWidget (self. Button_draw)
# self. SetLayout (layout)
Def the Draw (self) :
Print (income. Shape)

If __name__=="__main__" :
# every pyqt5 application must create an application object, sys. Argv parameters is a list of input parameters from the command line,
App=QApplication (sys. Argv)
MyWin=MyMainWindow ()
# shown on the screen
MyWin. The show ()
MyWin. PushButton_1. Clicked. Connect (Data_read)
Sys. Exit (app. Exec_ ())

Can read data, but the size of the second button cannot print data directly stuck then flash back, can change the draw on the inside to the other, with the income the data frame is not, I guess that he read less than income the data frame, but he didn't complain,
  • Related