In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 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 generate table pictures in Python". In the operation of actual cases, many people will encounter such a dilemma. Next, 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!
Pytab
Install first
Pip install pytab
Then I encapsulated the code for drawing the table into a function.
Import uuidfrom typing import List, Dict, Optional, Tupleimport pytabdef draw_table (data: Dict [str, list]): "drawing table: param data: data format {'asides: [1.0,2.1,3.5,4.0,2.0,2.1,3.5,4.0,2.0,],' baked: [5.7,6.1,7.2,8.3,1.2 5.7,6.1,7.2,8.3,1.2,],}: return: "" # set font Otherwise, the Chinese pytab.plt.rcParams ["font.sans-serif"] = ["SimHei"] pytab.table (data=data, data_loc='center', # th_type='dark', th_c='#aaaaee', # sets the header background color td_c='gray', # sets the data row background color table_type='striped' Figsize= (len (data.keys ()), int (len (data.values ()) / len (data.keys () + 1)), # fontsize=18,) # pytab.show () temp_file = os.path.join (tempfile.gettempdir (), f'{uuid.uuid4 () .hex} .jpg') print (temp_file) pytab.save (temp_file) return temp_file
According to the data format of the annotation, an and b are the header column names, and the following array is the data of each column, which is easy to understand.
{'averse: [1.0, 2.1, 3.5, 4.0, 2.0, 1.0, 2.1, 3.5, 4.0, 2.0,],' baked: [5.7, 6.1, 7.2, 8.3, 1.2, 5.7, 6.1, 7.2, 7.2, 8.3, 1.2,],}
The form drawn looks like this.
Let's try what happens when the Chinese display is displayed.
{'column 1': [1.0, 2.1, 3.5, 4.0, 2.0, 1.0, 2.1, 3.5, 4.0, 2.0,], 'column 2': [5.7, 6.1, 7.2, 8.3, 1.2, 5.7, 6.1, 7.2, 8.3, 1.2,],}
It's drawn like this.
It's a little ugly. I can barely see it.
OK~ doesn't have much trouble about pytab. After all, the upper limit is here.
Let's take a look at plotly.
Plotly
Install first
Pip install plotly
Needless to say, the old rule, I wrote it as a function again.
Import uuidfrom typing import List, Dict, Optional, Tupleimport plotly.graph_objects as goimport plotly.io as piodef draw_table (headers: List [str], cells: List [list]): "" drawing Table: param headers: header=dict (values= ['A Scores','B Scores']): param cells: cells=dict (values= [[100,90,80,90], [95,85,75,95]]): return: "" pio.kaleido.scope.default_width = len (' '.join (headers)) * 20 pio.kaleido.scope.default_height = 250 + len (cells [0]) * 20 fig = go.Figure (data= [go.Table (header=dict (values=headers), cells=dict (values=cells)]) # fig.show () image_file = os.path.join (tempfile.gettempdir (), f'{uuid.uuid4 () .hex} .jpg') print ('write image to', image_file) fig.write_image (image_file) return image_file
This time the parameter format is different from the previous pytab. Two parameters are passed, both of which are array types.
The first is the header, and the second array is the cell.
Let's try an example.
Draw_table (['column A', 'column B'], [[100,90,80,90], [95,85,75,95]])
Then remove the fig.show () comment in the above function code, and you can see the effect of the generated table picture.
After running, you can find that the browser opens automatically, because the library uses a web page to render the table.
The effect is as follows:
This is the end of the content of "how to generate Table Pictures by Python". Thank you for your 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.
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.