Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Pyecharts of Python to draw an image

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to draw an image with Python's Pyecharts". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Foreword:

Echarts is an open source data visualization JS tool from Baidu. The types of data visualization are very rich, but it has to be run on the Java Web project by importing the js library.

As a contestant who often uses Python in his work, he can't help knowing the power of this data visualization plug-in. So, is it possible to use the functions of Echarts in Python? During the search, I was pleasantly surprised to find pyecharts, just install the module in python and use it.

Installation:

Common pip installation packages install pyecharts with one click

Pyecharts installation commands:

Ython-m pip install pyecharts

Specific application of Python + pyecharts

Combined with the project data in work, I chose the data of hotel_code_new as CNSZVS_002,CWSWS_003 in test project requirements corresponding to RNs in 2019 to do visual display and analysis.

1.Hive database query sql

The hive_sql content is as follows:

Note that some of the syntax used in # sql is the regular syntax in hive sql, which is different from mysql. Select rrrd1.hotel_code_new as hotel_code_new, dda.natural_date as natural_date, nvl (rrrd.room_nights, 0) as room_nights from (select distinct substr (natural_dt) 1) as natural_date from dws.dws_test_date_calendar where dt_year='2019') dda left join (select 'CNSZVS_002' hotel_code_new UNION all select' CWSWS_003' hotel_code_new) rrrd1 left join (select hotel_code_new, substr (stay_date,1,7) as stay_date Sum (number_of_room_nights) as room_nights from dwm.dwm_test_resvs_rom_daily_df where dt='2021-10-24 'and hotel_code_new in (CNSZVS_002',' CWSWS_003') and resv_status in ('CHECKEDSSSIN','CHECKEDSSSOUT') and substr (stay_date) 0jue 4) = '2019' group by hotel_code_new,substr (stay_date,1,7)) rrrd on dda.natural_date = rrrd.stay_date and rrrd1.hotel_code_new=rrrd.hotel_code_new order by rrrd.hotel_code_new 2.Python code implementation-histogram from impala.dbapi import connectimport warnings# data warehouse data acquisition preparation def hive_connect (sql): warnings.filterwarnings ('ignore') config_hive_beta = {' host': '10.7.0.12, # hive host address' port': 10000, # hive port number 'user':' hive', # hive username 'password':' hive' # password 'database':' tmp' of hive Get} conn = connect (* * config_hive_beta) cursor = conn.cursor () cursor.execute (sql) hive_all_data = cursor.fetchall () return hive_all_data# all_data = hive_connect (hive_sql) # by calling hive in the hive-site.xml configuration file of the database name 'auth_mechanism':' PLAIN' # hive in # hive The database query result data obtained by the _ connect method is shown in the all_data list all_data = [('CNSZVS_002') '2019-01, 0), (' CNSZVS_002', '2019-02, 0), (' CNSZVS_002', '2019-03, 0), (' CNSZVS_002', '2019-04, 0), (' CNSZVS_002', '2019-05, 0), (' CNSZVS_002', '2019-06, 2353), (' CNSZVS_002', '2019-07, 2939) ('CNSZVS_002',' 2019-08, 5148), ('CNSZVS_002',' 2019-09, 3850), ('CNSZVS_002',' 2019-10, 4973), ('CNSZVS_002',' 2019-11, 5467), ('CNSZVS_002',' 2019-12, 4742), ('CWSWS_003',' 2019-01), ('CWSWS_003' '2019-02, 4434), (' CWSWS_003', '2019-03, 6003), (' CWSWS_003', '2019-04, 6611), (' CWSWS_003', '2019-05, 6586), (' CWSWS_003', '2019-06, 5840), (' CWSWS_003', '2019-07, 5840), (' CWSWS_003', '2019-08) ('CWSWS_003',' 2019-09, 5792), ('CWSWS_003',' 2019-10, 6898), ('CWSWS_003',' 2019-11, 6944), ('CWSWS_003',' 2019-12, 5404)] # Import bar chart from pyecharts module-Barfrom pyecharts import Bar# sets horizontal axis row name The abbreviations of 12 months are columns = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct" and "Nov". "Dec"] # create two new empty list to store the corresponding RNs values for each month CNSZVS_002 = [] CWSWS_003 = [] for i in all_data: if I [0] = 'CNSZVS_002': CNSZVS_002.append (I [2]) elif I [0] =' CWSWS_003': CWSWS_003.append (I [2]) else: pass# setting column The main title and subtitle of the chart bar = Bar ("bar chart") "Test requirements-RNs in 2019") # add data and configuration items for histograms-find the average, maximum and minimum bar.add ("CNSZVS_002", columns, CNSZVS_002, mark_line= ["average"], mark_point= ["max", "min"]) bar.add ("CWSWS_003", columns, CWSWS_003, mark_line= ["average"], mark_point= ["max") "min"]) # generate a local file named render.html under the same level directory of this py file (default is .html file) bar.render () # can also be set to the specified path to save the html file # bar.render (r "D:bar_render.html")

The column effect diagram shows:

The resulting column effect image is in html format, which can be opened and viewed in the browser. In the browser, it is supported to download the image format to the local, and click on the legend to gray the corresponding legend, while hiding the column chart data corresponding to the legend.

As shown in the following figure:

3.Python Code implementation-Pie Chart

Note: the code in the data preparation section is the same as the bar chart, and only the code specific to the pie chart is shown here.

# Import pie chart Piefrom pyecharts import Pie# from pyecharts module to set the main title and subtitle, set the title in the middle, set the width to 1000pie = Pie ("pie chart", "Test requirements-2019 RNs", title_pos='left', width=1000) # import data using add, and set the coordinate location to [20JE50] The colums option above cancels the display of pie.add ("CNSZVS_002", columns, CNSZVS_002, center= [20, 50], is_legend_show=True) # imports data using add, sets the coordinate location to [75jue 50], and the colums option above displays pie.add ("CWSWS_003", columns, CWSWS_003, center= [75jue 50], is_legend_show=False, is_label_show=True) # Save chart pie.render ()

Pie effect chart display-hidden percentage

Pie effect chart display-percentage of display

4.Python code implementation-box diagram # Import box diagram from pyecharts module Boxplotfrom pyecharts import Boxplotboxplot = Boxplot ("box diagram", "Test requirements-RNs 2019") x_axis = ['CNSZVS_002',' CWSWS_003'] y_axis = [CNSZVS_002, CWSWS_003] # prepare_data method can convert data to nested [min, Q1, median (or Q2), Q3 Max] yaxis = boxplot.prepare_data (y_axis) boxplot.add (RNs Statistics 2019, x_axis, yaxis) boxplot.render ()

The effect of the box chart is shown:

5.Python code implementation-line chart from pyecharts import Lineline = Line ("line chart", "Test requirements-RNs in 2019") # is_label_show property is to set whether the data above display line.add ("CNSZVS_002", columns, CNSZVS_002, is_label_show=True) line.add ("CWSWS_003", columns, CWSWS_003, is_label_show=True) line.render ()

Line chart effect display:

6.Python code implementation-radar map from pyecharts import Radarradar = Radar ("radar map", "Test requirements-2019 RNs") # since the data passed into the radar map has to be multidimensional data, the list needs to be converted to list again CNSZVS_002 = [CNSZVS_002] CWSWS_003 = [CWSWS_003] # set the maximum value of the column. In order to make the radar map more intuitive, the monthly maximum value is set according to the real data value. So each month varies schema_diff = [("Jan", 7000), ("Feb", 5000), ("Mar", 6500), ("Apr", 7000), ("May", 7000), ("Jun", 6200), ("Jul", 6800), ("Aug", 7200), ("Sep", 6000), ("Oct", 7300), ("Nov", 7500), ("Dec"). 6000)] # input coordinates radar.config (schema_diff) radar.add ("CNSZVS_002", CNSZVS_002) # generally defaults to the same color In order to make it easier to distinguish, you need to set the color of item radar.add ("CWSWS_003", CWSWS_003, item_color= "# 1C86EE") radar.render ()

Radar effect picture display:

7.Python code implementation-scatter chart from pyecharts import Scatterscatter = Scatter ("scatter chart", "Test requirements-2019 RNs") # xais_name is the setting Abscissa name, here due to the display problem You also need to set the distance between the y-axis name and the y-axis to scatter.add ("scatter distribution of CWSWS_003&CNSZVS_002 RNs", CNSZVS_002, CWSWS_003, xaxis_name= "CNSZVS_002", yaxis_name= "CWSWS_003", yaxis_name_gap=40) scatter.render ()

Scatter plot effect display:

This is the end of the content of "how to draw an image with Python's Pyecharts". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report