In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to display the images generated by pyecharts in pyqt5. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Through an actual scatter chart case, the following shows how to use pyqt5 to nest a pyecharts layer. Through this technique, you can also achieve exquisite data visualization function modules in the pyqt5 framework.
Technical background
Although few people use python to do some graphical interfaces, it has to be said that we still have the need for visualization and interaction in most of our daily software use. Therefore, pyqt5 as a mainstream python GUI framework status is very important, but also a very important skill. While pyecharts is equivalent to the python version of echarts, it is convenient to make some very beautiful visual pictures. Because the generated images are generally in html format, the mobility of the platform is relatively good. Here, we mainly explore the integration of a pyecharts-generated page in the interface created by pyqt5. The effect is as follows:
Environmental dependence
Here we mainly rely on pyecharts and pyqt5 libraries, but since pyqt5 has undergone great changes before and after version 5.10.1, it is recommended to install the latest version of pyqt5, and then install an additional component of QtWebEngineWidgets (you don't need to install it if you run the following program without any errors):
$python3-m pip show pyechartsName: pyechartsVersion: 1.9.1Summary: Python options, make charting easierHome-page: https://github.com/pyecharts/pyechartsAuthor: chenjiandongxAuthor-email: chenjiandongx@qq.comLicense: MITLocation: / home/dechin/miniconda3/lib/python3.9/site-packagesRequires: jinja2, prettytable SimplejsonRequired-by:$ python3-m pip show pyqt5Name: PyQt5Version: 5.15.6Summary: Python bindings for the Qt cross platform application toolkitHome-page: https://www.riverbankcomputing.com/software/pyqt/Author: Riverbank Computing LimitedAuthor-email: info@riverbankcomputing.comLicense: GPL v3Location: / home/dechin/miniconda3/lib/python3.9/site-packagesRequires: PyQt5-Qt5, PyQt5-sipRequired-by: PyQtWebEngine partial code parsing
Here we only extract a small part of the code for parsing, and this example is also modified by a framework found on the Internet. The first is the module of data generation:
Import numpy as npnums = 200data = np.random.random ((nums,2)) data = np.sort (data) x_data = data [:, 0] y_data = data [: 1]
Here we use numpy to generate a series of random numbers, and then sort them and then draw them in the form of Scatter scatter plot of pyecharts. When configuring the parameters of a scatter chart in pyecharts, the main method is to call the functions in Scatter to construct them. For example, some of our common window tools, region zooming and other functions, you can add a toolbox to Scatter to achieve:
Toolbox_opts=opts.ToolboxOpts (is_show=True, orient= "horizontal", feature=opts.ToolBoxFeatureOpts (save_as_image=opts.ToolBoxFeatureSaveAsImageOpts (type_= "jpeg", title= "Save as jpeg", pixel_ratio=2), restore=opts.ToolBoxFeatureRestoreOpts () Data_zoom=opts.ToolBoxFeatureDataZoomOpts (xaxis_index= [0], yaxis_index= [0]),)
This toolbox mainly realizes the function of saving web page as image, the function of region scaling, and the function of return operation. After you have constructed the layer through pyecharts, you need to:
Render ("/ tmp/scatter.html")
Method to save the generated effect diagram as a local html file Finally, the web page is imported into the layer in pyqt to achieve the display effect of the image:
Self.mainhboxLayout = QHBoxLayout (self) self.frame = QFrame (self) self.mainhboxLayout.addWidget (self.frame) self.hboxLayout = QHBoxLayout (self.frame) self.myHtml = QWebEngineView () self.myHtml.load (QUrl ("file:////tmp/scatter.html"))self.hboxLayout.addWidget(self.myHtml)self.setLayout(self.mainhboxLayout))
Overall code and display effect
The overall runnable code is as follows:
Import pyecharts.options as optsfrom pyecharts.charts import Scatterfrom PyQt5.QtCore import QUrlfrom PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QFramefrom PyQt5.QtWebEngineWidgets import QWebEngineViewimport sysimport numpy as npclass Stacked (QWidget): def _ _ init__ (self): super (Stacked, self). _ _ init__ () self.initData () self.initUI () self.mainLayout () def initUI (self): self.setGeometry (400400800) Self.setWindowTitle (") def initData (self): nums = 200data = np.random.random ((nums,2)) data = np.sort (data) x_data = data [:, 0] y_data = data [:, 1] (Scatter (init_opts=opts.InitOpts (width=" 1600px ") Height= "1000px") .add _ xaxis (xaxis_data=x_data) .add _ yaxis (series_name= "", y_axis=y_data, symbol_size=20, label_opts=opts.LabelOpts (is_show=False)) ) .set _ series_opts () .set _ global_opts (xaxis_opts=opts.AxisOpts (type_= "value", splitline_opts=opts.SplitLineOpts (is_show=True)), yaxis_opts=opts.AxisOpts (type_= "value") Axistick_opts=opts.AxisTickOpts (is_show=True), splitline_opts=opts.SplitLineOpts (is_show=True),), tooltip_opts=opts.TooltipOpts (is_show=False), toolbox_opts=opts.ToolboxOpts (is_show=True, orient= "horizontal" Feature=opts.ToolBoxFeatureOpts (save_as_image=opts.ToolBoxFeatureSaveAsImageOpts (type_= "jpeg", title= "Save as jpeg", pixel_ratio=2), restore=opts.ToolBoxFeatureRestoreOpts (), data_zoom=opts.ToolBoxFeatureDataZoomOpts (xaxis_index= [0]) Yaxis_index= [0]) Render ("/ tmp/scatter.html") def mainLayout (self): self.mainhboxLayout = QHBoxLayout (self) self.frame = QFrame (self) self.mainhboxLayout.addWidget (self.frame) self.hboxLayout = QHBoxLayout (self.frame) self.myHtml = QWebEngineView () # Open the local html file self.myHtml.load (QUrl ("file:////tmp/scatter.html")) self.hboxLayout.addWidget (self.myHtml) self.setLayout (self.mainhboxLayout) if _ _ name__ ='_ _ main__': app = QApplication (sys.argv) ex = Stacked () ex.show () sys.exit (app.exec_ ()
The effect of opening the interface is as follows:
By clicking the function button of region zoom, you can select a part of the area on the diagram for a more detailed display, and have a single-step return and one-step recovery function button. The display effect after selecting a portion is as follows:
The above is the editor for you to share how to display the images generated by pyecharts in pyqt5, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.