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 draw a rectangular Tree with pyecharts by python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how python uses pyecharts to draw a rectangular tree. I hope you will get something after reading this article. Let's discuss it together.

I. introduction of the concept

Rectangular tree diagram (Treemap), that is, rectangular tree structure diagram, uses the area of the rectangle to represent the size of the value, and the color is used to distinguish categories. It is often used to present multi-category one-dimensional numerical comparison, which is easy to read. Based on the function of the tree, the information of the data hierarchy can be presented at the same time in the structure diagram.

Examples are as follows:

Compared with common bar charts and bar charts, rectangular trees make up for the following three shortcomings:

1. When our data is multi-category and each category has only one value, we use histograms to waste a lot of space and appear monotonous.

2. When there is a big difference between the data (235 vs 18), it will bring trouble to the vertical coordinate setting of our histogram, and ignoring the range will dilute the difference of our small value class. (of course, if we only have one or two large values, we can take them out and deal with them.)

3. The column chart cannot present the hierarchical structure between the data.

Second, data display

Our goal is to show the numerical differences between classes, and if there is a second layer, by the way, compare the proportion of the second layer.

To show multiple categories, we use provincial data, which is shown in the excel table:

The second and third layers are randomly generated for tree presentation, and it is also to show that the tree layer structure does not require every node to have branches and leaves.

III. Data import

Let's first draw an one-dimensional one, only need to enter [province] [concern] two columns, this is actually a little more common (my point of view)

Province_type1 = pd.DataFrame (number of listed companies in each province / sample rectangular tree .xlsx') tree = [] name = [province_type1 ['province'] [I] +'\ n'+str (province_type1 ['concern A'] [I]) for i in range (len (province_type1))] for i in range (len (province_type1)): dic = {} dic ["value"] Dic ["name"] = int (province_type1 ['concern A'] [I]), name [I] tree.append (dic)

① name-- column phenotypic data structure, which is used to store the label of each data. Here I use a newline character in order to present the corresponding province and size of the data at the same time (if not, there is a line on our tree, which is not good-looking).

② needs a dictionary inside list,list to draw a rectangular tree, and the key name is specified as "name" and "value".

③ must note that if you draw a picture that has no data or is not displayed, check to see if there is a problem reading the excel data, that is, the location of the int in the above code.

The data structure used to draw the treemap is as follows:

4. Image rendering tm = (TreeMap () .add ("concern class A", tree) .set _ series_opts (label_opts=opts.LabelOpts (position='inside')) .set _ global_opts (title_opts=opts.TitleOpts (title ='', subtitle = '2022 ss') tm.render ('. / drawing result / rectangular tree-example .html')

Position--- specifies the label, that is, the location of our name, and inside will center the display. If not, the default top is displayed above each rectangle.

The results are as follows:

As can be seen from the picture above, Guangdong Province, Zhejiang Province and Jiangsu Province rank among the top three in random data. Due to the limitation of the display area, the rectangles with small data or long names often cannot be displayed completely, and their values need to be enlarged interactively.

5. Tree structure

After adding the tree structure, we need to add the data with key of "children" to the code accordingly.

From pyecharts.charts import Page,TreeMapfrom pyecharts import options as optsimport pandas as pdimport mathprovince_type1 = pd.DataFrame (pd.read_excel ('. / rectangular tree example .xlsx') tree = [] name = [province_type1 ['province'] [I] +'\ n'+str (province_type1 ['concern A'] [I]) for i in range (len (province_type1))] for i in range (len (province_type1)): dic = {} dic ["value"] Dic ["name"] = int (province_type1 ['concern class A'] [I]), name [I] if math.isnan (province_type1 ['concern class Amur1'] [I]) = = 0: dic ["children"] = [{"name": province_type1 [' province'] [I] + "Aly1:" + str (province_type1 ['concern class A'] [I]) "value": int (province_type1 ['Amur1'] [I])}, {"name": province_type1 [' province'] [I] + "Amuri 2:" + str (province_type1 ['Amur1'] [I]) "value": int (province_type1 ['Amur2'] [I])}] if math.isnan (province_type1 [' concern A1-1'] [I]) = = 0: dic ["children"] [0] ["children"] = [{"name": "A1-1:" + str (province_type1) ['concern class A1-1'] [I]) "value": int (province_type1 ['concern A1-1'] [I])}, {"name": "A1-2:" + str (province_type1 [' concern A1-1'] [I]) "value": int (province_type1 ['concern A1-2'] [I])}] tree.append (dic) tm = (TreeMap () .add ("Tree of concern A", tree) .set _ series_opts (label_opts=opts.LabelOpts (position='inside')) .set _ global_opts (title_opts=opts.TitleOpts (title =' come on, friends ~' Subtitle = '2022 shock 18-Lin old man ss') tm.render ('. / drawing result / rectangular tree-example 2. Html')

If there are no branches and leaves, there is no need to add "children" to the "name" and "value" level. if there are more than two categories in the branches and leaves, which is equivalent to a new tree, you need to add "children" with the same structure as the tree, as shown below:

["name": "parent node"

"value": number

"children": [{"name": "Node 1", "value": number}

{"name": "Node 2", "value": number}

{"name": "Sub-node 3", "value": number}

]

]

If the display area allows, the subsequence can continue to be added according to the above structure.

The result of the drawing is as follows:

From the figure above, we can see that although we named the parent class, the result displayed is only the name and value of the smallest class.

The pyecharts image is interactive, so you can enlarge the image by clicking on the focus class, but because you don't know how to present it to you, you are encouraged to explore it yourself.

After reading this article, I believe you have a certain understanding of "how to draw a rectangular tree with python with pyecharts". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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