Matplotlib draws images that meet the requirements of the thesis,
Recently, we need to plot the experiment data. Because we use python for the experiment, we naturally use matplotlib for plotting.
The following code can be used as the template code for drawing. The Code contains detailed notes and can be changed as needed.
1 #-*-coding: UTF-8-*-2 import numpy as np 3 import matplotlib. pyplot as plt 4 plt. rcParams ['font. sans-serif '] = ['arial'] # If you want to display a Chinese font, set it to SimHei 5 plt here. rcParams ['axes. unicode_minus '] = False # displays the negative number 6 7 x = np. array ([3, 5, 7, 9, 11, 13, 15, 17, 19,21]) 8 A = np. array ([0.9708, 0.6429, 1, 0.8333, 0.8841, 0.5867, 0.9352, 0.8000, 0.9359, 0.9405]) 9 B = np. array ([0.9708, 0.6558, 1, 0.8095, 0.8913, 0.5950, 0.9352, 0.8000, 0.9359, 0.9419]) 10 C = np. array ([0.9657, 0.6688, 0.9855, 0.7881, 0.8667, 0.5952, 0.9361, 0.7848, 0.9244, 0.9221]) 11 D = np. array ([0.9664, 0.6701, 0.9884, 0.7929, 0.8790, 0.6072, 0.9352, 0.7920, 0.9170, 0.9254]) 12 13 # label is shown in the figure (legend. If it is a mathematical formula, it is best to add the "$" symbol 14 # color: B: blue, g: green, r: red, c: cyan, m: magenta, y: yellow, k: black, w: white, 15 # line type :----. :, 16 # marker :., o v <* + 117 plt. figure (figsize = (10, 5) 18 plt. grid (linestyle = "--") # Set the background gridline to 19 x = plt. gca () 20 ax. spines ['top']. set_visible (False) # Remove the top frame 21 ax. spines ['right']. set_visible (False) # Remove the right border 22 23 plt. plot (x, A, color = "black", label = "A algorithm", linewidth = 1.5) 24 plt. plot (x, B, "k --", label = "B algorithm", linewidth = 1.5) 25 plt. plot (x, C, color = "red", label = "C algorithm", linewidth = 1.5) 26 plt. plot (x, D, "r --", label = "D algorithm", linewidth = 1.5) 27 28 group_labels = ['dataset1', 'dataset2', 'dataset3 ', 'dataset4', 'dataset5', 'dataset6', 'dataset7', 'dataset8', 'dataset9', 'dataset10'] # ID of the X axis scale 29 plt. xticks (x, group_labels, fontsize = 12, fontweight = 'bold ') # default font size: 1030 plt. yticks (fontsize = 12, fontweight = 'bold ') 31 plt. title ("example", fontsize = 12, fontweight = 'bold ') # default font size: 1232 plt. xlabel ("Data sets", fontsize = 13, fontweight = 'bold ') 33 plt. ylabel ("Accuracy", fontsize = 13, fontweight = 'bold ') 34 plt. xlim (3,21) # Set the X axis range to 35 # plt. ylim (0.5, 1) 36 37 # plt. legend () # Show the legends of each curve 38 plt. legend (loc = 0, numpoints = 1) 39 leg = plt. gca (). get_legend () 40 ltext = leg. get_texts () 41 plt. setp (ltext, fontsize = 12, fontweight = 'bold ') # Set the font size and width of the legend to 42 plt. savefig ('d: \ filename. svg ', format = 'svg') # It is recommended to save it as svg format, convert it to vector map emf with inkscape, and insert 44 plt in word. show ()
The following is an example of the code above:
We recommend that you save the image format svg (because matplotlib is faulty when it is saved as an eps vector image), and then convert the svg format to the emf vector image format using inkscape. If there are many svg images, you can use batch processing in windows (remember to set the path after the inkscape software is installed). The following code can convert the svg file under the directory to the emf file. Copy the following code to a text file and change the suffix to bat.
1 @echo off2 for %%i in (*.svg) do (3 echo %%i4 inkscape -f %%i -M %%~ni.emf5 )6 @echo Finished