Home>

This article introduces how to make radar charts in batches by using python.Let's take a look at how to achieve this.

Foreword

Because the job requires drawing radar charts sometimes,But what about a lot of data?Can't draw one by one excel, then you can use python for batch production,The resulting style is as follows:

First make a demo excel, score for excel random number generation:

1=int ((rand () + 4) * 10)/10

The excel styles obtained by adding tags, etc. are as follows (part, total 32 lines):

Then the next step is to open python to write code. This article is based onpycharm to write

wb=load_workbook (filename=r "c:\ users \ administrator \ desktop \ data indicator.
xlsx ") ##Read path
   ws=wb.get_sheet_by_name ("sheet1") ##Read sheet sheet named sheet1
   info_id=[]
   info_first=[]
   for row_a in range (2, 32):##Iterate through rows 2 to 32
     id=ws.cell (row=row_a, column=1) .value ##Iterate through rows 2 to 32, column 1
     info_id.append (id)
   for col in range (2, 9):##Read columns 1 to 9
     first=ws.cell (row=1, column=col) .value
     info_first.append (first) ##Get labels from 1 to 8 columns
   info_data=[]
   for row_num_btou in range (2, len (info_id) + 2):##Iterate through rows 2 to 32
     row_empty=[] ##Create an empty array as a temporary storage location,Every new line is cleared
     for i in range (2, 9):##Iterate through rows 2 to 32, and columns 2 to 9
       data_excel=ws.cell (row=row_num_btou, column=i) .value
       if data_excel == none:
         pass
       else:
         row_empty.append (data_excel) ##Store cell information in
     info_data.append (row_empty)

Step by step explanation:

Read the excel table:

Wb=load_workbook (filename=r "c:\ users \ administrator \ desktop \ data indicator.
xlsx ") ##Read path
   ws=wb.get_sheet_by_name ("sheet1") ##Read sheet sheet named sheet1

Need to use the library:

import xlsxwriter

from openpyxl import load_workbook

Under the command indicator, enter:

pip install xlsxwriter

Just wait for installation.The same goes for the following libraries:

Store the id of the first column and the label of the first row,The values ​​under the labels are stored separately:

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

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

workbook=xlsxwriter.workbook ("c:\\ users \\ administrator \\ desktop \\ result.xlsx")
   worksheet=workbook.add_worksheet () #create a worksheet object
   #font format
   font=workbook.add_format (
     {"border":1, "align":"center", "font_size":11, "font_name":"Microsoft Yahei"}) ##font centering,Number 11, Microsoft Yahei,For general information
   #Write the labels of the first row and first column
   worksheet.write (0, 0, "Product Code", font)
   ##Set the width of the column of the picture
   worksheet.set_column (0, len (info_first) + 1, 11) #Set the width of column len (info_first) + 1 to 11

Write label data, etc. into a new excel sheet:

#Create a new excel to save the results
   workbook=xlsxwriter.workbook ("c:\\ users \\ administrator \\ desktop \\ result.xlsx")
   worksheet=workbook.add_worksheet () #create a worksheet object
   #font format
   font=workbook.add_format (
     {"border":1, "align":"center", "font_size":11, "font_name":"Microsoft Yahei"}) ##font centering,Number 11, Microsoft Yahei,For general information
   #Write the labels of the first row and first column
   worksheet.write (0, 0, "Product Code", font)
   ##Set the width of the column of the picture
   worksheet.set_column (0, len (info_first) + 1, 11) #Set the width of column len (info_first) + 1 to 11
   ##Write tags
   for k in range (0,7):
     worksheet.write (0, k + 1, info_first [k], font)
   #Write the last column of labels
   worksheet.write (0, len (info_first) + 1, "radar chart", font)

Making a radar chart:

  #Set the name of each vertex of the radar
   labels=np.array (info_first)
   #Number of data
   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]])) #closed
     angles=np.concatenate ((angles, [angles [0]])) #closed
     fig=plt.figure ()
     ax=fig.add_subplot (111, polar=true) #polar parameter! !!
     ax.plot (angles, data, "bo-", linewidth=2) #draw lines
     ax.fill (angles, data, facecolor="r", alpha=0.25) #fill
     ax.set_thetagrids (angles * 180/np.pi, labels, fontproperties="simhei")
     ax.set_title ("Product Number:" + str (info_id [i]), va="bottom", fontproperties="simhei")
     ax.set_rlim (3.8,5) #Set the range of the radar chart
     ax.grid (true)
     plt.savefig ("c:\\ users \\ administrator \\ desktop \\ result \\ Product number:" + str (info_id [i]) + ".png", dpi=120)

What if the picture is too big?Just change the size with the library:

    import image
     ##Change picture size
     infile="c:\\ users \\ administrator \\ desktop \\ result \\ Product Number:" + str (info_id [i]) + ".png"
     outfile="c:\\ users \\ administrator \\ desktop \\ result1 \\ Product Number:" + 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)

Large and small images inresult and result1Two different folders,You need to create these two folders before:

  if os.path.exists (r "c:\\ users \\ administrator \\ desktop \\ result"):#Create a folder on the desktop,Folder for result
     print ("result folder already exists on the desktop,Continue to run the program ... ")
   else:
     print ("result folder is not on the desktop,New folder result ")
     os.mkdir (r "c:\\ users \\ administrator \\ desktop \\ result")
     print ("The folder was created successfully.
Continue to run the program ")
   if os.path.exists (r "c:\\ users \\ administrator \\ desktop \\ result1"):#Create a folder on the C drive, 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,New folder result1 ")
     os.mkdir (r "c:\\ users \\ administrator \\ desktop \\ result1")
     print ("The folder was created successfully.
Continue to run the program ")

Finally insert the picture into excel:

    worksheet.insert_image (i + 1, len (info_first) + 1, "c:\\ users \\ administrator \\ desktop \\ result1 \\" + "Product number:" + str (info_id [i]) + ". png ") ##Write image
     time.sleep (1) ##Prevent the computer from writing too fast
     plt.close () #Be sure to close the picture,Otherwise python will crash after opening 20 images
   workbook.close () #Finally close excel

The results obtained are as follows:

Attach the full code:

 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,Folder for result
     print ("result folder already exists on the desktop,Continue to run the program ... ")
   else:
     print ("result folder is not on the desktop,New folder result ")
     os.mkdir (r "c:\\ users \\ administrator \\ desktop \\ result")
     print ("The folder was created successfully.
Continue to run the program ")
   if os.path.exists (r "c:\\ users \\ administrator \\ desktop \\ result1"):#Create a folder on the C drive, 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,New folder result1 ")
     os.mkdir (r "c:\\ users \\ administrator \\ desktop \\ result1")
     print ("The folder was created successfully.
Continue to run the program ")
   wb=load_workbook (filename=r "c:\ users \ administrator \ desktop \ data indicator.
xlsx ") ##Read path
   ws=wb.get_sheet_by_name ("sheet1") ##Read sheet sheet named sheet1
   info_id=[]
   info_first=[]
   for row_a in range (2, 32):##Iterate through rows 2 to 32
     id=ws.cell (row=row_a, column=1) .value ##Iterate through rows 2 to 32, column 1
     info_id.append (id)
   for col in range (2, 9):##Read columns 1 to 9
     first=ws.cell (row=1, column=col) .value
     info_first.append (first) ##Get labels from 1 to 8 columns
   print (info_id)
   print (info_first)
   info_data=[]
   for row_num_btou in range (2, len (info_id) + 2):##Iterate through rows 2 to 32
     row_empty=[] ##Create an empty array as a temporary storage location,Every new line is cleared
     for i in range (2, 9):##Iterate through rows 2 to 32, and columns 2 to 9
       data_excel=ws.cell (row=row_num_btou, column=i) .value
       if data_excel == none:
         pass
       else:
         row_empty.append (data_excel) ##Store cell information in
     info_data.append (row_empty)
   print (info_data)
   print (len (info_data))
   #Set the name of each vertex of the radar
   labels=np.array (info_first)
   #Number of data
   data_len=len (info_first)
   #Create a new excel to save the result
   workbook=xlsxwriter.workbook ("c:\\ users \\ administrator \\ desktop \\ result.xlsx")
   worksheet=workbook.add_worksheet () #create a worksheet object
   #font format
   font=workbook.add_format (
     {"border":1, "align":"center", "font_size":11, "font_name":"Microsoft Yahei"}) ##font centering,Number 11, Microsoft Yahei,For general information
   #Write the labels of the first row and the first column
   worksheet.write (0, 0, "Product Code", font)
   ##Set the width of the column of the picture
   worksheet.set_column (0, len (info_first) + 1, 11) #Set the width of column len (info_first) + 1 to 11
   ##Write tags
   for k in range (0, 7):
     worksheet.write (0, k + 1, info_first [k], font)
   #Write the last column of labels
   worksheet.write (0, len (info_first) + 1, "radar chart", font)
   #Write other parameters to excel
   for j in range (0, len (info_id)):
     worksheet.write (j + 1, 0, info_id [j], font) #write the article number
     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) #Write other parameters of the product
   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]])) #closed
     angles=np.concatenate ((angles, [angles [0]])) #closed
     fig=plt.figure ()
     ax=fig.add_subplot (111, polar=true) #polar parameters! !!
     ax.plot (angles, data, "bo-", linewidth=2) #draw lines
     ax.fill (angles, data, facecolor="r", alpha=0.25) #fill
     ax.set_thetagrids (angles * 180/np.pi, labels, fontproperties="simhei")
     ax.set_title ("Product Number:" + str (info_id [i]), va="bottom", fontproperties="simhei")
     ax.set_rlim (3.8, 5) #Set the range of the radar chart
     ax.grid (true)
     plt.savefig ("c:\\ users \\ administrator \\ desktop \\ result \\ Product number:" + str (info_id [i]) + ".png", dpi=120)
     #plt.show () show in python
     ##Change picture size
     infile="c:\\ users \\ administrator \\ desktop \\ result \\ Product Number:" + str (info_id [i]) + ".png"
     outfile="c:\\ users \\ administrator \\ desktop \\ result1 \\ Article number:" + 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 Code:" + str (
                  info_id [i]) + ".png") ##Write image
     time.sleep (1) ##Prevent writing too fast and the computer freezes
     plt.close () #Be sure to close the picture,Otherwise python will crash after opening 20 images
   workbook.close () #Finally close excel
  • Previous Method for implementing custom Alert function style based on function overload in jQuery
  • Next js define object array (combination) multi-dimensional array method