Even people who do not specialize in programming will want to make graphs when they have data. The first thing that comes to mind for that is Excel. It is also widely used as graph prototyping, but although it can be enjoyed to some extent with macros, it is often frustrating because it is troublesome, slow, heavy, and inflexible in small details. Isn't it? Especially for an experimenter who has a lot of trials, it would be daunting to import data files for hundreds of trials into Excel one by one. Even more so, when you discover a small mistake in the legend of variables or graphs ...
Of course, there are also graph drawing software such as gnuplot that can be used relatively easily. But anyway, you want to use a fully customized graph generation application for yourself ...! No ... I want to make it! Well, I hope that it will be a guideline for such people, so I will publish various things together.
The content of this time is "Introduction and program overview".
Basically, the goal is to develop an "I am graph generation application" for experimenters. Specifically, it is an application that "reads a file in which measurement data is stored and draws a graph while changing variables in the edit section on the GUI". I think that I don't like the "I'm GUI" that I'm going to create, that the file format is different from the file format that the data I have, and that I don't need to change the variables, but I'll modify them as needed. Please "I". For the time being, the explanations of the PySide part (GUI generation part), PyQtGraph part (graph drawing part), and detailed processing part are separated as much as possible, so I will try to read only the part as needed.
Prerequisite knowledge is assumed to be "Basic knowledge of Python 3 (up to conditional branching / loop)". With "I" in mind, I give top priority to moving at hand, so the object-oriented way of thinking is passed through. I'll explain PySide and PyQtGraph in a little more detail.
To be honest, I'm a hobby programmer, so there are many things I don't understand. Please point out any mistakes.
Category:LanguageBindings::PySide - Qt Wiki PySide is a group of libraries that makes the C ++ cross-platform GUI framework "Qt" available from Python. There is also PyQt, but the license is different (GPL and LGPL). Qt is the newest (?) Cross-platform GUI framework for desktops, modern, cool and easy to use (appropriate). Originally, Nokia, the former owner of "Qt", started developing Qt4 so that it could be used in Python. After that, there was a lot of confusion, but recently there was a story on the mailing list that Autodesk, a major CAD software company, started to support the latest version of Qt5. If you follow Twitter with "PySide" as the search word, it seems that it is used quite a lot to extend Autodesk's 3D CG software "Maya". Many Autodesk products use Qt, so I think that's part of the connection. By the way, there are a lot of Japanese tweets.
PyQtGraph - Scientific Graphics and GUI Library for Python Perhaps the most famous Python graph drawing library is Matplotlib. Many comparison points are listed on the Official Site, but it is this PyQtGraph that was developed with an emphasis on the responsiveness of drawing, which is a weakness of Matplotlib. .. As PyQtGraph says, "Matplotlib is more mature, many people are using it, and it's better to use it over there," there seems to be some points that PyQtGraph lacks. However, it's only a matter of time since the discussions in the community are active. Personally, I think the best thing about PyQtGraph is that if you display two graphs side by side without adjusting with Matplotlib, the problem that the drawing area is covered or the axis label is missing as shown below does not occur. As the name suggests, it is a GUI library specialized for graph drawing, which is derived from Qt (PyQt / PySIde to be exact).
If you have the latest Python installed, you can install both at the command prompt (terminal / terminal) with the pip command. Maybe you need nampy when you put PyQtGraph. In that case, please use Anaconda or something.
pip install PySide
pip install PyQtGraph
In order to make the flow of explanations easier to understand in the future, I will explain the overall picture of the program for the time being. Create a Python source file with the following contents with a file name such as "graphApp.py".
graphApp.py
# [1]
import sys
import os
from PySide.QtCore import *
from PySide.QtGui import *
import pyqtgraph as pg
# [3]
class MainWindow(QWidget):
# [4]
#Initialization of MainWindow class(GUI generation, signal slot connection)
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
# [5]
#Update fileNameListWidget
def updateFileNameListWidget(self, fileNameListWidget, folderPath):
print('updateFileNameListWidget')
# [6]
#Graph generation function
def createGraph(self, folderPath, fileName):
print('createGraph')
# [6.1]
#Generate a set of data lists
def createDataListSet(self, folderPath, fileName):
print('createDataListSet')
# [6.2]
#Read a set of data lists
def loadDataListSet(self, folderPath, fileName):
print('loadDataListSet')
# [6.3]
#Generate a test graph
def createTestGraph(self, dataListSet):
print('createTestGraph')
# [2]
if __name__ == '__main__':
#Create a Qt Application
app = QApplication(sys.argv)
#Create and display a window
mainWin = MainWindow()
mainWin.show()
#Start the main loop of Qt
sys.exit(app.exec_())
I will explain it, including the preaching part in Buddha. A print () statement is written as a dummy in each function.
First, import the required modules and libraries in ** [1] **. The os module is used to read the file that stores the measurement data. About importing PySide and PyQtGraph
from PySide.QtCore import *
from PySide.QtGui import *
import pyqtgraph as pg
The reason is to reduce the amount of keystrokes. If you use Matplotlib, you need to import an extra library, but if you use PyQtGraph around here, it will be simple.
Then the ** [2] ** part is executed as the main function. As commented, but in the following places,
#Create and display a window
mainWin = MainWindow()
mainWin.show()
Creates and displays a window called mainWin.
The MainWindow () class, which is mainWin, inherits QWwidget and initializes it with QWwidget, as you can see from ** [3] ** and ** [4] **. Next time, in ** [4] **, we will describe GUI elements such as a list widget for displaying the file list, a text box that describes the directory of the list to be displayed, and a graph drawing area. ..
** [5] updateFileNameListWidget () ** is a function to display the file list in the list widget mentioned above.
** [6] createGraph () ** is a cushion function for generating a data list and drawing a graph based on it. The data list is created with ** [6.1] createDataListSet () ** and ** [6.2] loadDataListSet () **. Specifically, once ** [6.2] ** is called, the measurement data stored in the file is read, and the data list obtained in ** [6.1] ** is shaped by multiplying it by a coefficient and returned. It will be. Draw a graph by throwing this formatted data list to ** [6.3] createTestGraph () **.
The above is the overall picture of the program to be created.
By the way, in fact, it can be executed as a program with a GUI as it is. Please open the directory where "graphApp.py" is located and execute it as follows. I think a clean window will open.
cd 「graphApp.Directory where "py" is located
python graphApp.py
From the next time onward, I plan to grow the previous program into a little over 300 lines of code. Next time, we will do GUI programming with PySide in [4] \ _ \ _ init \ _ \ _ ().
Recommended Posts