How to Create a radar chart in batches using python and a python radar chart

Source: Internet
Author: User
Tags radar

How to Create a radar chart in batches using python and a python radar chart

Preface

Sometimes radar charts are needed for work, but what if there are many groups of data? You cannot click "excel" to draw images one by one. You can use python to batch create images. The style is as follows:

First, create a demo excel, and score the excel random number generation:

1 =INT((RAND()+4)*10)/10 

The excel Style obtained by adding tags is as follows (partial, total 32 rows ):

The next step is to open the python code. This article is based onpycharmWrite

 

Wb = load_workbook (filename = r 'C: \ Users \ Administrator \ Desktop \ data indicator .xlsx') # Read path ws = wb. get_sheet_by_name ("Sheet1") # Read the table info_id = [] info_first = [] for row_A in range (2, 32) with the name of Sheet1 ): # traverse 2nd rows to 32 rows id = ws. cell (row = row_A, column = 1 ). value ## traverse 2nd rows to 32 rows, 1st columns info_id.append (id) for col in range (2, 9): ## read 1st to 9 columns first = ws. cell (row = 1, column = col ). value info_first.append (first) # obtain the info_data = [] for row_num_BtoU in range (2, len (info_id) + 2) label in columns 1 to 8 ): # traverse 2nd rows to 32 rows row_empty = [] # create an empty array as a temporary storage location. Each line feed is cleared for I in range (2, 9 ): # traverse 2nd rows to 32 rows, 2nd to 9 columns data_excel = ws. cell (row = row_num_BtoU, column = I ). value if data_excel = None: pass else: row_empty.append (data_excel) # store the cell information in info_data.append (row_empty)

Step-by-step explanation:

Read an excel table:

Wb = load_workbook (filename = r 'C: \ Users \ Administrator \ Desktop \ data indicator .xlsx') # Read path ws = wb. get_sheet_by_name ("Sheet1") # Read the table Sheet1


Databases required:

 import xlsxwriter

 from openpyxl import load_workbook

Enter the following command indicator:

 pip install xlsxwriter

Wait for the installation. The following libraries are also used:

Store the ID of the first column and the tags of the first row. The values under the tags are stored in:

  info_id = []  info_first = []  info_data = []

After reading the data, you need to set the write format:

Workbook = xlsxwriter. workbook ('C: \ Users \ Administrator \ Desktop \ result.xlsx') worksheet = workbook. add_worksheet () # create a worksheet object # font = workbook in font format. add_format ({'border': 1, 'align ': 'center', 'font _ size': 11, 'font _ name': ' '}) # font center, No. 11,, for general information # Write the label worksheet in the first column of the first line. write (0, 0, 'commodity detail', font) # Set the width of the worksheet column of the image. set_column (0, len (info_first) + 1, 11) # Set the width of column len (info_first) + 1 to 11

Write tag data to a new excel file:

# Create an excel save result workbook = xlsxwriter. workbook ('C: \ Users \ Administrator \ Desktop \ result.xlsx') worksheet = workbook. add_worksheet () # create a worksheet object # font = workbook in font format. add_format ({'border': 1, 'align ': 'center', 'font _ size': 11, 'font _ name': ' '}) # font center, No. 11,, for general information # Write the label worksheet in the first column of the first line. write (0, 0, 'commodity detail', font) # Set the width of the worksheet column of the image. set_column (0, len (info_first) + 1, 11) # Set the width of column len (info_first) + 1 to 11 # Write the label for k in range ): worksheet. write (0, k + 1, info_first [k], font) # write the last label worksheet. write (0, len (info_first) + 1, 'radar fig', font)

Make a radar chart:

# Set labels = np for each radar vertex. array (info_first) # data Count data_len = len (info_first) for I in range (0, len (info_id): data = np. array (info_data [I]) angles = np. linspace (0, 2 * np. pi, data_len, endpoint = False) data = np. concatenate (data, [data [0]) # Close angles = np. concatenate (angles, [angles [0]) # closes fig = plt. figure () ax = fig. add_subplot (111, polar = True) # polar parameter !! Ax. plot (angles, data, 'bo-', linewidth = 2) # Draw line ax. fill (angles, data, facecolor = 'R', alpha = 0.25) # fill in ax. set_thetagrids (angles * 180/np. pi, labels, fontproperties = "SimHei") ax. set_title ("item detail:" + str (info_id [I]), va = 'bottom ', fontproperties = "SimHei") ax. set_rlim (3.8, 5) # Set the radar chart range to ax. grid (True) plt. savefig ("C: \ Users \ Administrator \ Desktop \ result \ product detail:" + str (info_id [I]) + ". png ", dpi = 120)

What if the image is too big?Use the database to change the size.:

Import Image # change Image size infile = "C: \ Users \ Administrator \ Desktop \ result \ product detail:" + str (info_id [I]) + ". png "outfile =" C: \ Users \ Administrator \ Desktop \ result1 \ product detail: "+ str (info_id [I]) + ". png "im = Image. open (infile) (x, y) = im. size x_s = 80 # set length y_s = 100 # Set width out = im. resize (x_s, y_s), Image. ANTIALIAS) out. save (outfile, 'png ', quality = 95)

Place large and small imagesResult and result1For two different folders, you need to create these two folders on the front:

If OS. path. exists (r 'C: \ Users \ Administrator \ Desktop \ result'): # create a folder on the Desktop, the folder is result print ('result folder already exists on the desktop, continue to run the program ...... ') Else: print ('result folder is not on the desktop, create folder result') OS. mkdir (r 'C: \ Users \ Administrator \ Desktop \ result') print ('Folder created successfully, continue running Project') if OS. path. exists (r 'C: \ Users \ Administrator \ Desktop \ result1 '): # create a folder on disk C, the folder is result1 print ('result1 folder already exists on the desktop, continue to run the program ...... ') Else: print ('result1 folder is not on the desktop, create a folder named 'result1') OS. mkdir (r 'C: \ Users \ Administrator \ Desktop \ result1 ') print ('Folder created successfully, continue to running ')

Insert the image to the excel file:

Worksheet. insert_image (I + 1, len (info_first) + 1, 'c: \ Users \ Administrator \ Desktop \ result1 \ '+ "product detail: "+ str (info_id [I]) + '.png ') # write image time. sleep (1) # prevent writing too fast computer crashes plt. close () # Be sure to turn off the image. Otherwise, the workbook will crash when python opens 20 images. close () # close excel

The result is as follows:

Complete code is attached:

Import numpy as np import matplotlib. pyplot as plt import xlsxwriter from openpyxl import load_workbook import OS import time from PIL import Image if _ name _ = '_ main _': if OS. path. exists (r 'C: \ Users \ Administrator \ Desktop \ result'): # create a folder on the Desktop, the folder is result print ('result folder already exists on the desktop, continue to run the program ...... ') Else: print ('result folder is not on the desktop, create folder result') OS. mkdir (r 'C: \ Users \ Administrator \ Desktop \ result') print ('Folder created successfully, continue running Project') if OS. path. exists (r 'C: \ Users \ Administrator \ Desktop \ result1 '): # create a folder on disk C, the folder is result1 print ('result1 folder already exists on the desktop, continue to run the program ...... ') Else: print ('result1 folder is not on the desktop, create a folder named 'result1') OS. mkdir (r 'C: \ Users \ Administrator \ Desktop \ result1 ') print ('Folder created successfully, continue to running ') wb = load_workbook (filename = r 'C: \ Users \ Administrator \ Desktop \ data indicator .xlsx') # Read path ws = wb. get_sheet_by_name ("Sheet1") # Read the table info_id = [] info_first = [] for row_A in range (2, 32) with the name of Sheet1 ): # traverse 2nd rows to 32 rows id = ws. cell (row = row_A, column = 1 ). value ## traverse 2nd rows to 32 rows and 1st columns of info_id.appe Nd (id) for col in range (2, 9): ## read first = ws from column 1st to column 9. cell (row = 1, column = col ). value info_first.append (first) # obtain the print (info_id) print (info_first) info_data = [] for row_num_BtoU in range (2, len (info_id) + 2) label of Columns 1 to 8): ## traverse 2nd rows to 32 rows row_empty = [] # create an empty array as a temporary storage location. Each line feed is cleared for I in range (2, 9 ): # traverse 2nd rows to 32 rows, 2nd to 9 columns data_excel = ws. cell (row = row_num_BtoU, column = I ). value if data_excel = None: pass else: r Ow_empty.append (data_excel) # store the cell information in info_data.append (row_empty) print (info_data) print (len (info_data) # Set the name of each radar vertex labels = np. array (info_first) # Data Count data_len = len (info_first) # create a new excel save result workbook = xlsxwriter. workbook ('C: \ Users \ Administrator \ Desktop \ result.xlsx') worksheet = workbook. add_worksheet () # create a worksheet object # font = workbook in font format. add_format ({'border': 1, 'align ': 'center ', 'Font _ size': 11, 'font _ name': ''}) # font center, No. 11,, for general information, use # to write down the label worksheet in the first column of the first row. write (0, 0, 'commodity detail', font) # Set the width of the worksheet column of the image. set_column (0, len (info_first) + 1, 11) # Set the width of column len (info_first) + 1 to 11 # Write the label for k in range (0, 7): worksheet. write (0, k + 1, info_first [k], font) # write the last label worksheet. write (0, len (info_first) + 1, 'radar fig', font) # write Other parameters to the excel file for j in range (0, len (info_id): wor Ksheet. write (j + 1, 0, info_id [j], font) # write the product into worksheet. set_row (j, 76) # Set the row width for x in range (0, len (info_first): worksheet. write (j + 1, x + 1, info_data [j] [x], font) # other parameters for writing products for I in range (0, len (info_id )): data = np. array (info_data [I]) angles = np. linspace (0, 2 * np. pi, data_len, endpoint = False) data = np. concatenate (data, [data [0]) # Close angles = np. concatenate (angles, [angles [0]) # Close fig = plt. figure () ax = fig. add_subplot (111, polar = True) # polar parameter !! Ax. plot (angles, data, 'bo-', linewidth = 2) # Draw line ax. fill (angles, data, facecolor = 'R', alpha = 0.25) # fill in ax. set_thetagrids (angles * 180/np. pi, labels, fontproperties = "SimHei") ax. set_title ("item detail:" + str (info_id [I]), va = 'bottom ', fontproperties = "SimHei") ax. set_rlim (3.8, 5) # Set the radar chart range to ax. grid (True) plt. savefig ("C: \ Users \ Administrator \ Desktop \ result \ product detail:" + str (info_id [I]) + ". png ", dpi = 120) # plt. show () is displayed in python # change the image size infile = "C: \ Users \ Administrator \ Desktop \ result \ product detail: "+ str (info_id [I]) + ". png "outfile =" C: \ Users \ Administrator \ Desktop \ result1 \ product detail: "+ str (info_id [I]) + ". png "im = Image. open (infile) (x, y) = im. size x_s = 80 # set length y_s = 100 # Set width out = im. resize (x_s, y_s), Image. ANTIALIAS) out. save (outfile, 'png ', quality = 95) worksheet. insert_image (I + 1, len (info_first) + 1, 'c: \ Users \ Administrator \ Desktop \ result1 \ '+ "product detail: "+ str (info_id [I]) + '.png ') # write image time. sleep (1) # prevent writing too fast computer crashes plt. close () # Be sure to turn off the image. Otherwise, the workbook will crash when python opens 20 images. close () # close excel

This article describes how to use python to create radar charts in batches.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.