matplotlib繪制符合論文要求的圖片實例(必看篇)
最近需要將實驗數(shù)據(jù)畫圖出來,由于使用python進行實驗,自然使用到了matplotlib來作圖。
下面的代碼可以作為畫圖的模板代碼,代碼中有詳細注釋,可根據(jù)需要進行更改。
# -*- 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)中顯示。若為數(shù)學公式,則最好在字符串前后添加"$"符號 #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 = "--") #設置背景網(wǎng)格線為虛線 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()
下面是上面代碼繪制的圖例:
建議保存圖片的格式svg(因為matplotlib存為eps矢量圖時候會有問題),然后使用inkscape軟件將svg格式轉為emf矢量圖格式。如果svg圖片很多,可以在windows下使用批處理(安裝inkscape軟件后要記得設置path路徑),下面這段代碼能將它所在目錄下的svg文件轉為emf文件。將下面代碼復制到文本文件,改后綴名為bat。
@echo off for %%i in (*.svg) do ( echo %%i inkscape -f %%i -M %%~ni.emf ) @echo Finished
以上這篇matplotlib繪制符合論文要求的圖片實例(必看篇)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python實現(xiàn)精準搜索并提取網(wǎng)頁核心內容
這篇文章主要為大家介紹了python實現(xiàn)精準搜索并提取網(wǎng)頁核心內容的實現(xiàn),有需要的的朋友可以借鑒參考下,希望能有所幫助祝大家多多進步2021-11-11詳解Pandas中stack()和unstack()的使用技巧
當你在處理包含某種序列(例如時間序列數(shù)據(jù))的變量的數(shù)據(jù)集時,數(shù)據(jù)通常需要進行重塑。Pandas?提供了各種用于重塑?DataFrame?的內置方法。其中,stack()?和?unstack()?是最流行的,本文總結了這兩個方法的7種使用技巧,需要的可以參考一下2022-03-03OpenCV python sklearn隨機超參數(shù)搜索的實現(xiàn)
這篇文章主要介紹了OpenCV python sklearn隨機超參數(shù)搜索的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01