1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams[ 'font.sans-serif' ] = [ 'Arial' ] #如果要顯示中文字體,則在此處設爲:SimHei
plt.rcParams[ 'axes.unicode_minus' ] = False #顯示負號
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在圖示(legend)中顯示。若爲數學公式,則最好在字符串前後添加"$"符號
#color:b:blue、g:green、r:red、c:cyan、m:magenta、y:yellow、k:black、w:white、、、
#線型:- -- -. : ,
#marker:. , o v < * + 1
plt.figure(figsize = ( 10 , 5 ))
plt.grid(linestyle = "--" ) #設置背景網格線爲虛線
ax = plt.gca()
ax.spines[ 'top' ].set_visible( False ) #去掉上邊框
ax.spines[ 'right' ].set_visible( False ) #去掉右邊框
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' ] #x軸刻度的標識
plt.xticks(x,group_labels,fontsize = 12 ,fontweight = 'bold' ) #默認字體大小爲10
plt.yticks(fontsize = 12 ,fontweight = 'bold' )
plt.title( "example" ,fontsize = 12 ,fontweight = 'bold' ) #默認字體大小爲12
plt.xlabel( "Data sets" ,fontsize = 13 ,fontweight = 'bold' )
plt.ylabel( "Accuracy" ,fontsize = 13 ,fontweight = 'bold' )
plt.xlim( 3 , 21 ) #設置x軸的範圍
#plt.ylim(0.5,1)
#plt.legend() #顯示各曲線的圖例
plt.legend(loc = 0 , numpoints = 1 )
leg = plt.gca().get_legend()
ltext = leg.get_texts()
plt.setp(ltext, fontsize = 12 ,fontweight = 'bold' ) #設置圖例字體的大小和粗細
plt.savefig( 'D:\\filename.svg' , format = 'svg' ) #建議保存爲svg格式,再用inkscape轉爲矢量圖emf後插入word中
plt.show()
|