In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "Python machine how to use Pyecharts to make a visual screen", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "how to use Pyecharts in the Python machine to make a visual screen" it!
Catalogue
Preface
Pyecharts visualization
Map World Map
Bar chart, pie chart
Pyecharts composite chart
Preface
ECharts is a commercial data chart library based on JS, which is open source by Baidu. There are many ready-made chart types and examples, while Pyecharts is to facilitate us to use Python to achieve ECharts drawing. Using Pyecharts to create a large visual screen can be divided into two steps:
1. Use separate Pyecharts to make all kinds of graphics.
2. Use the combined chart function in Pyecharts to display all the pictures in a html file.
Pyecharts visualization
This article reduces the chart, using only the distribution map of gold medals of various countries in the 2020 Tokyo Olympic Games, the details of the medal table of the 2020 Tokyo Olympic Games, and the winning details of China's various events in the 2020 Tokyo Olympic Games.
This kind of chart is very simple and can be learned by copying the example directly with reference to the official documentation. Chart color matching uses the Pyecharts default color, we try to form their own style when we actually use it.
Crawl data
Import requestsimport pandas as pdfrom pprint import pprinturl = 'https://app-sc.miguvideo.com/vms-livedata/olympic-medal/total-table/15/110000004609'data1 = requests.get (url). Json () pprint (data1) Map World Map
When Pyecharts maps the world, the name must be in English. So in the previous article, we introduced the Chinese and English comparison table of country names, and the left join formed df1:
Df1 = pd.DataFrame () for info in data1 ['body'] [' allMedalData']: name = info ['countryName'] name_id = info [' countryId'] rank = info ['rank'] gold = info [' goldMedalNum'] silver = info ['silverMedalNum'] bronze = info [' bronzeMedalNum'] total = info ['totalMedalNum'] # Organization data orangized_data = [[name,name_id,rank,gold,silver,bronze] Total]] # and then add df df1 = df1.append (orangized_data) df1.columns = ['name', 'ID',' ranking', 'gold', 'silver', 'bronze', 'total medals'] df1
Url = 'https://app-sc.miguvideo.com/vms-livedata/olympic-medal/detail-total/15/110000004609'data2 = requests.get (url). Json () pprint (data2) df2 = pd.DataFrame () for info in data2 [' body'] ['medalTableDetail']: english_name = info [' countryName'] name_id = info ['countryId'] award_time = info [' awardTime'] item_name = info ['bigItemName'] sports _ name = info ['sportsName'] medal_type = info [' medalType'] # Organization data orangized_data = [[english_name Name_id,award_time,item_name,sports_name,medal_type]] # then add df df2 = df2.append (orangized_data) df2.columns = ['acronym', 'ID',' award time', 'project name', 'athlete', 'gold medal type'] df2
Data preprocessing
With open ("country name Chinese and English comparison table .txt", "r", encoding= "utf-8") as f: X = f.read () df3 = pd.DataFrame () for i in x.split ("\ n"): X = i.split (":") [0] .strip () y = i.split (":") [1] .strip () orangined_data = [x Y] df3 = df3.append (orangined_data) df3.columns = ["name", "English name"] df3.to_excel ("country name comparison table .xlsx", index=None) df4 = pd.merge (df1,df3,on= "name", how= "left") df4.head (10)
Df5 = df2df6 = pd.merge (df4,df5,on= "ID", how= "left") df6.head (10)
Df6
X = {"ranking of winners": ["gold medal", "silver medal", "bronze medal"], "gold medal type": [1pm 2je 3]} df7 = pd.DataFrame (x) df7
Df8 = pd.merge (df6,df7,on= "Gold Type", how= "left") df8.head (10)
Df4.head (10)
Pyecharts visualization
Two columns of data of English name and total number of medals are extracted separately for visualization.
Data_list= [[iMagnej] for iMagnej in zip (df4 ['English name'], df4 ['Total medals'])] data_list [: 5]
When the data is ready, we begin to use pyecharts to map the world.
From pyecharts import options as optsfrom pyecharts.charts import Mapc = (Map () .add ("", data_list, "world", is_map_symbol_show=False,) .set _ series_opts (label_opts=opts.LabelOpts (is_show=False)) .set _ global_opts (title_opts=opts.TitleOpts (title= "Total Medal Distribution Map of the 2020 Tokyo Olympic Games") Visualmap_opts=opts.VisualMapOpts (max_=100) c.render_notebook ()
It's simple
Total gold medals
Data_list1= [for iMagol j in zip (df4 ['English name'], df4 ['Gold'])] data_list1 [: 5] from pyecharts import options as optsfrom pyecharts.charts import Mapc = (Map () .add (", data_list1," world ", is_map_symbol_show=False) ) .set _ series_opts (label_opts=opts.LabelOpts (is_show=False)) .Set _ global_opts (title_opts=opts.TitleOpts (title= "Distribution Map of Gold medals at the 2020 Tokyo Olympic Games"), visualmap_opts=opts.VisualMapOpts (max_=100)) c.render_notebook ()
Total number of silver medals
Data_list2= [for iMagol j in zip (df4 ['English name'], df4 ['Silver'])] data_list2 [: 5] from pyecharts import options as optsfrom pyecharts.charts import Mapc = (Map () .add (", data_list2," world ", is_map_symbol_show=False) ) .set _ series_opts (label_opts=opts.LabelOpts (is_show=False)) .Set _ global_opts (title_opts=opts.TitleOpts (title= "Silver Distribution Map of the 2020 Tokyo Olympic Games"), visualmap_opts=opts.VisualMapOpts (max_=100)) c.render_notebook ()
Bar chart, pie chart
Bar chart (Bar)
From pyecharts import options as optsfrom pyecharts.charts import Barc = (Bar () .add _ xaxis (df4 ['name'] .head (25). Tolist ()) .add _ yaxis ("gold", df4 ['gold'] .head (25). Tolist (), stack= "stack1") .add _ yaxis ("silver", df4 ['silver'] .head (25). Tolist (), stack= "stack1") .add _ yaxis ("bronze") Df4 ['bronze'] .head (25). Tolist (), stack= "stack1") .set _ series_opts (label_opts=opts.LabelOpts (is_show=True, position= "inside", font_size=12, color='#FFFFFF')) .set _ global_opts (title_opts=opts.TitleOpts (title= "2020 Tokyo Olympic Games medals details"), xaxis_opts=opts.AxisOpts (type_='category' Axislabel_opts=opts.LabelOpts (rotate=45),)) c.render_notebook ()
Pie chart (Pie)
From pyecharts import options as optsfrom pyecharts.charts import Piec = (Pie () .add ("", [[diving, 12], [shooting, 11], [weightlifting, 8], [table tennis, 7], [swimming, 6], [badminton, 6], [track and field, 5], [still water kayak, 3] [trampoline gymnastics, 3], [freestyle wrestling, 3], [rowing, 3], [karate, 2], [boxing, 2], [synchronized swimming, 2], [taekwondo, 1], [track cycling, 1], [Greco-Roman wrestling, 1], [fencing, 1] ['three-man basketball', 1]], center= ["50%", "60%"],) .set _ series_opts (label_opts=opts.LabelOpts (formatter= "{b}: {c}")) c.render_notebook ()
In this way, the three charts that need to be used will be drawn.
Pyecharts composite chart
The second step for Pyecharts to visualize a large screen is to combine charts, which can be roughly divided into four categories:
Grid: parallel multi-graph
Page: sequential multiple graphs
Tab: tab multi-graph
Timeline: timeline broadcast multi-graph
Official document: Pyecharts
Page (sequential multi-graph) is mostly used here, and before combining the chart, we should change the previous chart drawing code into a function.
Def map_world ()-> Map: C = (Map (init_opts=opts.InitOpts (chart_id=2, bg_color='#ADD8E6')) .add (", data_list," world ", is_map_symbol_show=False) ) .set _ series_opts (label_opts=opts.LabelOpts (is_show=False)) .Set _ global_opts (title_opts=opts.TitleOpts (title= "Distribution Map of Gold medals at the 2020 Tokyo Olympic Games"), visualmap_opts=opts.VisualMapOpts (max_=100) return c
By the way, the background color bg_color and chart IDchart_id are added, which are used to distinguish the location of multiple charts. For the background color, I chose light blue # ADD8E6. The layout of the subsequent pictures is based on the corresponding relationship of the chart ID, so each picture should set its id separately.
Then use page = Page (layout= Page.DraggablePageLayout) mode to display the picture, this step is to adjust the layout.
Page = Page (layout=Page.DraggablePageLayout, page_title= "2020 Tokyo Olympic Games Medal Table") # add chart page.add (title (), map_world (), bar_medals (), pie_china (),) page.render ('test.html') to the page
A test.html file is generated after calling the drawing function.
After opening it, you can drag and drop the pictures to achieve a custom layout.
After the layout of the picture is finished, remember to click "save config" in the upper left corner to save the layout file.
When clicked, a chart_config.json file is generated locally, which contains the layout location corresponding to each chart ID.
Finally, the saved layout file is called to regenerate the html.
Run the following line of code.
Page.save_resize_html ('test.html', cfg_file='chart_config.json', dest=' Olympic .html')
Where test.html is the file of all generated charts, chart_config.json is the downloaded layout file, Olympic .html is the layout dashboard file, open instrument Olympic .html:
In this way, a data visualization-large screen display is realized.
But there are still many shortcomings, such as if the chart color matching is not special to make adjustments.
The whole big screen is just a static display, not a data dashboard with a business scene.
Real large data screens often prefer to be generated by BI software, which can realize cross-screening among graphs, tables and slicers. I hope to have the opportunity to use Python to make them in the future.
Thank you for your reading, the above is the content of "how to use Pyecharts to make a visual screen in a Python machine". After the study of this article, I believe you have a deeper understanding of how to use Pyecharts to make a visual screen in a Python machine. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.