Matplotlib: Drawing Image instances that meet the requirements of the thesis (mandatory ),
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.
#-*-Coding: UTF-8-*-import numpy as npimport matplotlib. pyplot as pltplt. rcParams ['font. sans-serif'] = ['arial'] # If you want to display a Chinese font, set it to SimHeiplt. rcParams ['axes. unicode_minus '] = False # displays the negative number x = np. array ([3, 5, 7, 9, 11, 13, 15, 17, 19,21]) A = np. array ([0.9708, 0.6429, 1, 0.8333, 0.8841, 0.5867, 0.9352, 0.8000, 0.9359, 0.9405]) B = np. array ([0.9708, 0.6558, 1, 0.8095, 0.8913, 0.5950, 0.9352, 0.8000, 0.9359, 0.9419]) C = np. array ([0.9657, 0.6688, 0.9855, 0.7881, 0.8667, 0.5952, 0.9361, 0.7848, 0.9244, 0.9221]) D = np. array ([0.9664, 0.6701, 0.9884, 0.7929, 0.8790, 0.6072, 0.9352, 0.7920, 0.9170, 0.9254]) # label is displayed in the figure (legend. If it is a mathematical formula, it is best to add the "$" symbol # color: B: blue, g: green, r: red, c: cyan, m: magenta, y: yellow, k: black, w: white, # line type :----. :, # marker :., o v <* + 1plt. figure (figsize = (10, 5) plt. grid (linestyle = "--") # Set the background gridline to the dotted line ax = plt. gca () ax. spines ['top']. set_visible (False) # Remove the top frame ax. spines ['right']. set_visible (False) # Remove the right border plt. plot (x, A, color = "black", label = "A algorithm", linewidth = 1.5) plt. plot (x, B, "k --", label = "B algorithm", linewidth = 1.5) plt. plot (x, C, color = "red", label = "C algorithm", linewidth = 1.5) plt. plot (x, D, "r --", label = "D algorithm", linewidth = 1.5) group_labels = ['dataset1', 'dataset2', 'dataset3 ', 'dataset4', 'dataset5', 'dataset6', 'dataset7', 'dataset8', 'dataset9', 'dataset10'] # identify the plt of the X axis scale. xticks (x, group_labels, fontsize = 12, fontweight = 'bold ') # The default font size is 10plt. yticks (fontsize = 12, fontweight = 'bold ') plt. title ("example", fontsize = 12, fontweight = 'bold ') # The default font size is 12plt. xlabel ("Data sets", fontsize = 13, fontweight = 'bold ') plt. ylabel ("Accuracy", fontsize = 13, fontweight = 'bold ') plt. xlim (3,21) # Set the X axis range # plt. ylim (0.5, 1) # plt. legend () # displays the legend plt of each curve. legend (loc = 0, numpoints = 1) leg = plt. gca (). get_legend () ltext = leg. get_texts () plt. setp (ltext, fontsize = 12, fontweight = 'bold ') # Set the font size and width of the legend 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 the 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.
@echo off for %%i in (*.svg) do ( echo %%i inkscape -f %%i -M %%~ni.emf ) @echo Finished
The above matplotlib image examples that meet the requirements of the paper (which must be read) are all the content shared by Alibaba Cloud xiaobian. I hope you can provide a reference and support for the customer's house.