Python-matplotlib用法
安裝matplotlib:
????????pip install matplotlib
help doc:
Basic Usage — Matplotlib 3.5.1 documentationhttps://matplotlib.org/stable/tutorials/introductory/usage.html推薦文章:
??????? 字體:【Python基礎(chǔ)】matplotlib字體設(shè)置看這一篇就夠了_fengdu78的博客-CSDN博客
??????? 繪圖:Python繪圖總結(jié)(Matplotlib篇)之字體、文本及注釋_wuzlun的專欄-CSDN博客_matplotlib 字體
1 基礎(chǔ)繪圖
直接上代碼:
(1)代碼中打印出了一些默認(rèn)的格式或樣式
(2)代碼中進(jìn)行了樣式的設(shè)置示例
#導(dǎo)入Matplotlib模塊 #from matplotlib import pyplot as plt import matplotlib.pyplot as plt import matplotlib.font_manager as FontManager import pandas as pd import numpy as np#打印圖標(biāo)樣式,看看自帶的樣式有哪些 print(f"matplotlib支持的默認(rèn)格式:\n{plt.style.available}") print(f"windows系統(tǒng)支持的字體:\n{FontManager._get_win32_installed_fonts()}") print(f"matplotlib支持的樣式:\n{plt.style.available}") for font in FontManager.fontManager.ttflist:# 查看字體名以及對應(yīng)的字體文件名print(font.name, '-', font.fname)#導(dǎo)入數(shù)據(jù) xvalues1=[1,2,3,4,5] ysquares1=[x**2 for x in xvalues1] xvalues2=[1,2,3,4,5] ysquares2=[x+2 for x in xvalues2] xvalues3=[1,2,3,4,5] ysquares3=[x**2-2 for x in xvalues3] ysquares4=[x**2+2 for x in xvalues3]#設(shè)置圖表風(fēng)格,需要在畫圖前設(shè)置好,否則無效 plt.style.use('_mpl-gallery') #plt.rcParams['font.sans-serif']=['Times New Roman'] # 用來正常顯示中文標(biāo)簽 plt.rcParams['axes.unicode_minus']=False # 用來正常顯示負(fù)號 plt.rcParams["font.family"] = "Times New Roman"#設(shè)置圖中所有的字體#fig表示整張圖篇,ax表示圖片中的對應(yīng)圖表,如果是兩張圖則實(shí)例是ax[0],ax[1] fig1,ax=plt.subplots(2,1,figsize=(5, 2.7), layout='constrained')#一個圖中畫上下一個表'''#在圖表ax[0]中畫圖''' al1=ax[0].plot(xvalues1,ysquares1,color='black',linestyle='solid',marker='o',label='Square',linewidth=3) al2=ax[0].plot(xvalues2,ysquares2,color='red',linestyle='dashdot',marker= 'x',label='line',linewidth=1) ax[0].legend()#顯示前面添加的標(biāo)簽label,可以傳遞[al1,al2],['Square',"Line"]#設(shè)置圖表顯示的標(biāo)簽和坐標(biāo)軸 ax[0].set_title("Simple Plot",fontsize=20,color='red') ax[0].set_xlabel("值",fontsize=10,fontproperties="Microsoft YaHei") ax[0].set_ylabel("值的平方",fontsize=10,fontproperties="Microsoft YaHei") ax[0].axis([0,6,0,40])#設(shè)置坐標(biāo)軸范圍也可以用ax.set_ylim(0, 40); ax[0].tick_params(axis='both',which='major',labelsize=8)#設(shè)置刻度字體大小 #以x軸為橫坐標(biāo)畫出數(shù)據(jù)的分布 n, bins, patches = ax[0].hist(xvalues1, 50, density=1, facecolor='C0', alpha=0.75)'''ax[1]中畫散點(diǎn)圖,S設(shè)置尺寸''' ax[1].set_title("Simple scatter",fontsize=20,color='red') #顏色設(shè)置可以采用RGB的形式(0, 0.8, 0),值越接近 0,指定的顏色越深;值越接近 1,指定的顏色越淺 ax[1].scatter(xvalues3, ysquares3, s=100, facecolor=(0, 0.8, 0), edgecolor='blue');#畫散點(diǎn)圖 #顏色映射(colormap)是一系列顏色,從起始顏色漸變到結(jié)束顏色,此處通過Y軸的坐標(biāo)值進(jìn)行映射 ax[1].scatter(xvalues3, ysquares4, s=200, c=ysquares4,cmap=plt.cm.Blues); #對特定的點(diǎn)進(jìn)行注釋,xy=箭頭的坐標(biāo),xytext=文字的坐標(biāo) ax[1].annotate('annotate test', xy=(3, 10), xytext=(3.5, 20),arrowprops=dict(facecolor='black', shrink=0.05))'''在圖中打印一行字(1.5,15.5)為文字左邊的坐標(biāo)(ha='left')''' plt.text(1.5,15.5,"this is a 測試",fontsize=15,ha='center',fontproperties=FontManager.FontProperties(fname='C:\Windows\Fonts\simfang.ttf') )'''保存圖象''' #bbox_inches裁剪空白區(qū)域 plt.savefig('Simple_plot.png', bbox_inches='tight') #顯示圖表 plt.show()打印的默認(rèn)格式和樣式,可以看到畫圖支持的樣式和支持的字體:
matplotlib支持的默認(rèn)格式: ['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10'] matplotlib支持的字體: {WindowsPath('C:/Windows/Fonts/LFAXD.TTF'), WindowsPath('C:/Windows/Fonts/STKAITI.TTF'), WindowsPath('C:/Windows/Fonts/gadugib.ttf'), WindowsPath('C:/Windows/Fonts/CENTAUR.TTF'), WindowsPath('C:/Windows/Fonts/MAGNETOB.TTF'), WindowsPath('C:/Windows/Fonts/FTLTLT.TTF'), WindowsPath('C:/Windows/Fonts/ariali.ttf'), WindowsPath('C:/Windows/Fonts/BERNHC.TTF'), WindowsPath('C:/Windows/Fonts/FORTE.TTF'), WindowsPath('C:/Windows/Fonts/palai.ttf'), WindowsPath('C:/Windows/Fonts/GARAIT.TTF'), WindowsPath('C:/Windows/Fonts/mingliub.ttc'), WindowsPath('C:/Windows/Fonts/consola.ttf'), WindowsPath('C:/Windows/Fonts/arialbi.ttf'), WindowsPath('C:/Windows/Fonts/FRABKIT.TTF'), WindowsPath('C:/Windows/Fonts/LeelawUI.ttf'), WindowsPath('C:/Windows/Fonts/BRITANIC.TTF'), WindowsPath('C:/Windows/Fonts/taile.ttf'), WindowsPath('C:/Windows/Fonts/comicz.ttf'), WindowsPath('C:/Windows/Fonts/BOD_CR.TTF'), WindowsPath('C:/Windows/Fonts/ARIALNB.TTF'), WindowsPath('C:/Windows/Fonts/sseriff.fon'), WindowsPath('C:/Windows/Fonts/impact.ttf'), WindowsPath('C:/Windows/Fonts/ROCKEB.TTF'), WindowsPath('C:/Windows/Fonts/malgun.ttf'), WindowsPath('C:/Windows/Fonts/PERTIBD.TTF'), WindowsPath('C:/Windows/Fonts/lucon.ttf'), WindowsPath('C:/Windows/Fonts/simkai.ttf'), WindowsPath('C:/Windows/Fonts/Dengl.ttf'), WindowsPath('C:/Windows/Fonts/ITCKRIST.TTF'), WindowsPath('C:/Windows/Fonts/GOUDOS.TTF'), WindowsPath('C:/Windows/Fonts/GILLUBCD.TTF'), WindowsPath('C:/Windows/Fonts/LBRITEI.TTF'), WindowsPath('C:/Windows/Fonts/gadugi.ttf'), WindowsPath('C:/Windows/Fonts/modern.fon'), WindowsPath('C:/Windows/Fonts/BRADHITC.TTF'), WindowsPath('C:/Windows/Fonts/MEIRYO.TTC'), WindowsPath('C:/Windows/Fonts/segoesc.ttf'), WindowsPath('C:/Windows/Fonts/LeelUIsl.ttf'), WindowsPath('C:/Windows/Fonts/segoeuib.ttf'), WindowsPath('C:/Windows/Fonts/STXIHEI.TTF'), WindowsPath('C:/Windows/Fonts/CALIFR.TTF'), WindowsPath('C:/Windows/Fonts/phagspa.ttf'), WindowsPath('C:/Windows/Fonts/SIMLI.TTF'), WindowsPath('C:/Windows/Fonts/TCCB____.TTF'), WindowsPath('C:/Windows/Fonts/ARIALNBI.TTF'), WindowsPath('C:/Windows/Fonts/OLDENGL.TTF'), WindowsPath('C:/Windows/Fonts/72-Black.ttf'), WindowsPath('C:/Windows/Fonts/courf.fon'), WindowsPath('C:/Windows/Fonts/constanz.ttf'), WindowsPath('C:/Windows/Fonts/ARIALN.TTF'), WindowsPath('C:/Windows/Fonts/sapdn.ttf'), WindowsPath('C:/Windows/Fonts/palabi.ttf'), WindowsPath('C:/Windows/Fonts/OCRAEXT.TTF'), WindowsPath('C:/Windows/Fonts/FRADMCN.TTF'), WindowsPath('C:/Windows/Fonts/NIAGSOL.TTF'), WindowsPath('C:/Windows/Fonts/arial.ttf'), WindowsPath('C:/Windows/Fonts/GILB____.TTF'), WindowsPath('C:/Windows/Fonts/72-Italic.ttf'), WindowsPath('C:/Windows/Fonts/LTYPEB.TTF'), WindowsPath('C:/Windows/Fonts/MATURASC.TTF'), WindowsPath('C:/Windows/Fonts/calibriz.ttf'), WindowsPath('C:/Windows/Fonts/PERBI___.TTF'), WindowsPath('C:/Windows/Fonts/GILC____.TTF'), WindowsPath('C:/Windows/Fonts/Gabriola.ttf'), WindowsPath('C:/Windows/Fonts/cambriai.ttf'), WindowsPath('C:/Windows/Fonts/simsunb.ttf'), WindowsPath('C:/Windows/Fonts/CENTURY.TTF'), WindowsPath('C:/Windows/Fonts/PAPYRUS.TTF'), WindowsPath('C:/Windows/Fonts/Dengb.ttf'), WindowsPath('C:/Windows/Fonts/REFSAN.TTF'), WindowsPath('C:/Windows/Fonts/BOD_R.TTF'), WindowsPath('C:/Windows/Fonts/POORICH.TTF'), WindowsPath('C:/Windows/Fonts/LCALLIG.TTF'), WindowsPath('C:/Windows/Fonts/PRISTINA.TTF'), WindowsPath('C:/Windows/Fonts/trebucit.ttf'), WindowsPath('C:/Windows/Fonts/seriff.fon'), WindowsPath('C:/Windows/Fonts/GOTHICB.TTF'), WindowsPath('C:/Windows/Fonts/ntailu.ttf'), WindowsPath('C:/Windows/Fonts/PALSCRI.TTF'), WindowsPath('C:/Windows/Fonts/LTYPEO.TTF'), WindowsPath('C:/Windows/Fonts/MEIRYOB.TTC'), WindowsPath('C:/Windows/Fonts/BOD_CBI.TTF'), WindowsPath('C:/Windows/Fonts/LeelaUIb.ttf'), WindowsPath('C:/Windows/Fonts/SCHLBKBI.TTF'), WindowsPath('C:/Windows/Fonts/timesbd.ttf'), WindowsPath('C:/Windows/Fonts/seguisli.ttf'), WindowsPath('C:/Windows/Fonts/GIL_____.TTF'), WindowsPath('C:/Windows/Fonts/HTOWERT.TTF'), WindowsPath('C:/Windows/Fonts/BASKVILL.TTF'), WindowsPath('C:/Windows/Fonts/mmrtext.ttf'), WindowsPath('C:/Windows/Fonts/JUICE___.TTF'), WindowsPath('C:/Windows/Fonts/GILSANUB.TTF'), WindowsPath('C:/Windows/Fonts/GOTHICBI.TTF'), WindowsPath('C:/Windows/Fonts/CURLZ___.TTF'), WindowsPath('C:/Windows/Fonts/MISTRAL.TTF'), WindowsPath('C:/Windows/Fonts/LBRITE.TTF'), WindowsPath('C:/Windows/Fonts/MSUIGHUR.TTF'), WindowsPath('C:/Windows/Fonts/seguisbi.ttf'), WindowsPath('C:/Windows/Fonts/arialbd.ttf'), WindowsPath('C:/Windows/Fonts/LEELAWAD.TTF'), WindowsPath('C:/Windows/Fonts/FRADM.TTF'), WindowsPath('C:/Windows/Fonts/FRABK.TTF'), WindowsPath('C:/Windows/Fonts/calibril.ttf'), WindowsPath('C:/Windows/Fonts/Candara.ttf'), WindowsPath('C:/Windows/Fonts/wingding.ttf'), WindowsPath('C:/Windows/Fonts/Sitka.ttc'), WindowsPath('C:/Windows/Fonts/segoepr.ttf'), WindowsPath('C:/Windows/Fonts/calibri.ttf'), WindowsPath('C:/Windows/Fonts/couri.ttf'), WindowsPath('C:/Windows/Fonts/ERASLGHT.TTF'), WindowsPath('C:/Windows/Fonts/COPRGTB.TTF'), WindowsPath('C:/Windows/Fonts/verdanai.ttf'), WindowsPath('C:/Windows/Fonts/COPRGTL.TTF'), WindowsPath('C:/Windows/Fonts/malgunsl.ttf'), WindowsPath('C:/Windows/Fonts/trebucbd.ttf'), WindowsPath('C:/Windows/Fonts/72-Bold.ttf'), WindowsPath('C:/Windows/Fonts/72-CondensedBold.ttf'), WindowsPath('C:/Windows/Fonts/HATTEN.TTF'), WindowsPath('C:/Windows/Fonts/msyi.ttf'), WindowsPath('C:/Windows/Fonts/YuGothL.ttc'), WindowsPath('C:/Windows/Fonts/YuGothB.ttc'), WindowsPath('C:/Windows/Fonts/ITCEDSCR.TTF'), WindowsPath('C:/Windows/Fonts/TCBI____.TTF'), WindowsPath('C:/Windows/Fonts/ERASDEMI.TTF'), WindowsPath('C:/Windows/Fonts/ARIALNI.TTF'), WindowsPath('C:/Windows/Fonts/BOOKOSB.TTF'), WindowsPath('C:/Windows/Fonts/tahoma.ttf'), WindowsPath('C:/Windows/Fonts/georgiaz.ttf'), WindowsPath('C:/Windows/Fonts/Nirmala.ttf'), WindowsPath('C:/Windows/Fonts/corbelz.ttf'), WindowsPath('C:/Windows/Fonts/SitkaI.ttc'), WindowsPath('C:/Windows/Fonts/BRLNSR.TTF'), WindowsPath('C:/Windows/Fonts/CALISTB.TTF'), WindowsPath('C:/Windows/Fonts/LATINWD.TTF'), WindowsPath('C:/Windows/Fonts/YuGothM.ttc'), WindowsPath('C:/Windows/Fonts/ebrima.ttf'), WindowsPath('C:/Windows/Fonts/ARIALUNI.TTF'), WindowsPath('C:/Windows/Fonts/TCM_____.TTF'), WindowsPath('C:/Windows/Fonts/SNAP____.TTF'), WindowsPath('C:/Windows/Fonts/comicbd.ttf'), WindowsPath('C:/Windows/Fonts/msjhl.ttc'), WindowsPath('C:/Windows/Fonts/COLONNA.TTF'), WindowsPath('C:/Windows/Fonts/ROCCB___.TTF'), WindowsPath('C:/Windows/Fonts/BOOKOSBI.TTF'), WindowsPath('C:/Windows/Fonts/ebrimabd.ttf'), WindowsPath('C:/Windows/Fonts/ntailub.ttf'), WindowsPath('C:/Windows/Fonts/georgiab.ttf'), WindowsPath('C:/Windows/Fonts/ROCK.TTF'), WindowsPath('C:/Windows/Fonts/LFAX.TTF'), WindowsPath('C:/Windows/Fonts/ELEPHNT.TTF'), WindowsPath('C:/Windows/Fonts/HARNGTON.TTF'), WindowsPath('C:/Windows/Fonts/SCHLBKB.TTF'), WindowsPath('C:/Windows/Fonts/BOD_BLAR.TTF'), WindowsPath('C:/Program Files (x86)/Common Files/Microsoft Shared/EQUATION/MTEXTRA.TTF'), WindowsPath('C:/Windows/Fonts/corbeli.ttf'), WindowsPath('C:/Windows/Fonts/ONYX.TTF'), WindowsPath('C:/Windows/Fonts/segmdl2.ttf'), WindowsPath('C:/Windows/Fonts/cambria.ttc'), WindowsPath('C:/Windows/Fonts/constani.ttf'), WindowsPath('C:/Windows/Fonts/GOTHICI.TTF'), WindowsPath('C:/Windows/Fonts/msyh.ttc'), WindowsPath('C:/Windows/Fonts/roman.fon'), WindowsPath('C:/Windows/Fonts/BOD_I.TTF'), WindowsPath('C:/Windows/Fonts/FRADMIT.TTF'), WindowsPath('C:/Windows/Fonts/comic.ttf'), WindowsPath('C:/Windows/Fonts/BAUHS93.TTF'), WindowsPath('C:/Windows/Fonts/LEELAWDB.TTF'), WindowsPath('C:/Windows/Fonts/segoeuii.ttf'), WindowsPath('C:/Windows/Fonts/BOD_CB.TTF'), WindowsPath('C:/Windows/Fonts/webdings.ttf'), WindowsPath('C:/Windows/Fonts/georgia.ttf'), WindowsPath('C:/Windows/Fonts/mmrtextb.ttf'), WindowsPath('C:/Windows/Fonts/STXINGKA.TTF'), WindowsPath('C:/Windows/Fonts/ERASMD.TTF'), WindowsPath('C:/Windows/Fonts/SCHLBKI.TTF'), WindowsPath('C:/Windows/Fonts/verdana.ttf'), WindowsPath('C:/Windows/Fonts/javatext.ttf'), WindowsPath('C:/Windows/Fonts/72-Regular.ttf'), WindowsPath('C:/Windows/Fonts/smallf.fon'), WindowsPath('C:/Windows/Fonts/segoeuisl.ttf'), WindowsPath('C:/Windows/Fonts/BRUSHSCI.TTF'), WindowsPath('C:/Windows/Fonts/STCAIYUN.TTF'), WindowsPath('C:/Windows/Fonts/72-Condensed.ttf'), WindowsPath('C:/Windows/Fonts/segoeuil.ttf'), WindowsPath('C:/Windows/Fonts/segoescb.ttf'), WindowsPath('C:/Windows/Fonts/BRLNSB.TTF'), WindowsPath('C:/Windows/Fonts/STZHONGS.TTF'), WindowsPath('C:/Windows/Fonts/Candaraz.ttf'), WindowsPath('C:/Windows/Fonts/FRSCRIPT.TTF'), WindowsPath('C:/Windows/Fonts/ANTQUAB.TTF'), WindowsPath('C:/Windows/Fonts/HTOWERTI.TTF'), WindowsPath('C:/Windows/Fonts/TCCEB.TTF'), WindowsPath('C:/Windows/Fonts/SHOWG.TTF'), WindowsPath('C:/Windows/Fonts/ROCC____.TTF'), WindowsPath('C:/Windows/Fonts/YuGothR.ttc'), WindowsPath('C:/Windows/Fonts/pala.ttf'), WindowsPath('C:/Windows/Fonts/MTCORSVA.TTF'), WindowsPath('C:/Windows/Fonts/taileb.ttf'), WindowsPath('C:/Windows/Fonts/corbelli.ttf'), WindowsPath('C:/Windows/Fonts/BELL.TTF'), WindowsPath('C:/Windows/Fonts/LTYPEBO.TTF'), WindowsPath('C:/Windows/Fonts/SCRIPTBL.TTF'), WindowsPath('C:/Windows/Fonts/consolab.ttf'), WindowsPath('C:/Windows/Fonts/LSANS.TTF'), WindowsPath('C:/Windows/Fonts/timesbi.ttf'), WindowsPath('C:/Windows/Fonts/Candaral.ttf'), WindowsPath('C:/Windows/Fonts/SitkaZ.ttc'), WindowsPath('C:/Windows/Fonts/l_10646.ttf'), WindowsPath('C:/Windows/Fonts/calibrii.ttf'), WindowsPath('C:/Windows/Fonts/framdit.ttf'), WindowsPath('C:/Windows/Fonts/Candarab.ttf'), WindowsPath('C:/Windows/Fonts/OUTLOOK.TTF'), WindowsPath('C:/Windows/Fonts/LBRITED.TTF'), WindowsPath('C:/Windows/Fonts/AGENCYR.TTF'), WindowsPath('C:/Windows/Fonts/corbel.ttf'), WindowsPath('C:/Windows/Fonts/msyhbd.ttc'), WindowsPath('C:/Windows/Fonts/seguihis.ttf'), WindowsPath('C:/Windows/Fonts/times.ttf'), WindowsPath('C:/Windows/Fonts/GOUDOSI.TTF'), WindowsPath('C:/Windows/Fonts/ANTQUABI.TTF'), WindowsPath('C:/Windows/Fonts/RAVIE.TTF'), WindowsPath('C:/Windows/Fonts/LSANSI.TTF'), WindowsPath('C:/Windows/Fonts/GOUDOSB.TTF'), WindowsPath('C:/Windows/Fonts/LSANSDI.TTF'), WindowsPath('C:/Windows/Fonts/corbelb.ttf'), WindowsPath('C:/Windows/Fonts/CHILLER.TTF'), WindowsPath('C:/Windows/Fonts/ELEPHNTI.TTF'), WindowsPath('C:/Windows/Fonts/BROADW.TTF'), WindowsPath('C:/Windows/Fonts/simsun.ttc'), WindowsPath('C:/Windows/Fonts/LTYPE.TTF'), WindowsPath('C:/Windows/Fonts/segoeprb.ttf'), WindowsPath('C:/Windows/Fonts/cour.ttf'), WindowsPath('C:/Windows/Fonts/LBRITEDI.TTF'), WindowsPath('C:/Windows/Fonts/STSONG.TTF'), WindowsPath('C:/Windows/Fonts/serife.fon'), WindowsPath('C:/Windows/Fonts/BSSYM7.TTF'), WindowsPath('C:/Windows/Fonts/BELLI.TTF'), WindowsPath('C:/Windows/Fonts/sylfaen.ttf'), WindowsPath('C:/Windows/Fonts/FRAMDCN.TTF'), WindowsPath('C:/Windows/Fonts/FRAHV.TTF'), WindowsPath('C:/Windows/Fonts/INFROMAN.TTF'), WindowsPath('C:/Windows/Fonts/GARA.TTF'), WindowsPath('C:/Windows/Fonts/seguibl.ttf'), WindowsPath('C:/Windows/Fonts/CALISTI.TTF'), WindowsPath('C:/Windows/Fonts/timesi.ttf'), WindowsPath('C:/Windows/Fonts/FREESCPT.TTF'), WindowsPath('C:/Windows/Fonts/FZSTK.TTF'), WindowsPath('C:/Windows/Fonts/ERASBD.TTF'), WindowsPath('C:/Windows/Fonts/courbd.ttf'), WindowsPath('C:/Windows/Fonts/cambriaz.ttf'), WindowsPath('C:/Windows/Fonts/BOD_CI.TTF'), WindowsPath('C:/Windows/Fonts/FELIXTI.TTF'), WindowsPath('C:/Windows/Fonts/CENSCBK.TTF'), WindowsPath('C:/Windows/Fonts/BOD_B.TTF'), WindowsPath('C:/Windows/Fonts/TCCM____.TTF'), WindowsPath('C:/Windows/Fonts/trebucbi.ttf'), WindowsPath('C:/Windows/Fonts/GOTHIC.TTF'), WindowsPath('C:/Windows/Fonts/VLADIMIR.TTF'), WindowsPath('C:/Windows/Fonts/ENGR.TTF'), WindowsPath('C:/Windows/Fonts/seguiemj.ttf'), WindowsPath('C:/Windows/Fonts/72-BoldItalic.ttf'), WindowsPath('C:/Windows/Fonts/SIMYOU.TTF'), WindowsPath('C:/Windows/Fonts/RAGE.TTF'), WindowsPath('C:/Windows/Fonts/msgothic.ttc'), WindowsPath('C:/Windows/Fonts/LFAXDI.TTF'), WindowsPath('C:/Windows/Fonts/sapin.ttf'), WindowsPath('C:/Windows/Fonts/AGENCYB.TTF'), WindowsPath('C:/Windows/Fonts/BOOKOS.TTF'), WindowsPath('C:/Windows/Fonts/PLAYBILL.TTF'), WindowsPath('C:/Windows/Fonts/REFSPCL.TTF'), WindowsPath('C:/Windows/Fonts/mvboli.ttf'), WindowsPath('C:/Windows/Fonts/BKANT.TTF'), WindowsPath('C:/Windows/Fonts/GLSNECB.TTF'), WindowsPath('C:/Windows/Fonts/georgiai.ttf'), WindowsPath('C:/Windows/Fonts/FRAHVIT.TTF'), WindowsPath('C:/Windows/Fonts/LSANSD.TTF'), WindowsPath('C:/Windows/Fonts/NirmalaS.ttf'), WindowsPath('C:/Windows/Fonts/comici.ttf'), WindowsPath('C:/Windows/Fonts/courbi.ttf'), WindowsPath('C:/Windows/Fonts/FZYTK.TTF'), WindowsPath('C:/Windows/Fonts/malgunbd.ttf'), WindowsPath('C:/Windows/Fonts/symbol.ttf'), WindowsPath('C:/Windows/Fonts/script.fon'), WindowsPath('C:/Windows/Fonts/BOD_PSTC.TTF'), WindowsPath('C:/Windows/Fonts/msjhbd.ttc'), WindowsPath('C:/Windows/Fonts/sserife.fon'), WindowsPath('C:/Windows/Fonts/ariblk.ttf'), WindowsPath('C:/Windows/Fonts/JOKERMAN.TTF'), WindowsPath('C:/Windows/Fonts/BOD_BI.TTF'), WindowsPath('C:/Windows/Fonts/ITCBLKAD.TTF'), WindowsPath('C:/Windows/Fonts/SAPGUI-icons.ttf'), WindowsPath('C:/Windows/Fonts/MAIAN.TTF'), WindowsPath('C:/Windows/Fonts/ROCKI.TTF'), WindowsPath('C:/Windows/Fonts/WINGDNG3.TTF'), WindowsPath('C:/Windows/Fonts/segoeuiz.ttf'), WindowsPath('C:/Windows/Fonts/CALIST.TTF'), WindowsPath('C:/Windows/Fonts/STFANGSO.TTF'), WindowsPath('C:/Windows/Fonts/verdanaz.ttf'), WindowsPath('C:/Windows/Fonts/GILI____.TTF'), WindowsPath('C:/Windows/Fonts/bahnschrift.ttf'), WindowsPath('C:/Windows/Fonts/framd.ttf'), WindowsPath('C:/Windows/Fonts/IMPRISHA.TTF'), WindowsPath('C:/Windows/Fonts/constanb.ttf'), WindowsPath('C:/Windows/Fonts/GIGI.TTF'), WindowsPath('C:/Windows/Fonts/tahomabd.ttf'), WindowsPath('C:/Windows/Fonts/CASTELAR.TTF'), WindowsPath('C:/Windows/Fonts/seguisym.ttf'), WindowsPath('C:/Windows/Fonts/STLITI.TTF'), WindowsPath('C:/Windows/Fonts/GOUDYSTO.TTF'), WindowsPath('C:/Windows/Fonts/ANTQUAI.TTF'), WindowsPath('C:/Windows/Fonts/calibrili.ttf'), WindowsPath('C:/Windows/Fonts/simfang.ttf'), WindowsPath('C:/Windows/Fonts/segoeui.ttf'), WindowsPath('C:/Windows/Fonts/cambriab.ttf'), WindowsPath('C:/Windows/Fonts/Candarali.ttf'), WindowsPath('C:/Windows/Fonts/CALISTBI.TTF'), WindowsPath('C:/Windows/Fonts/CALIFI.TTF'), WindowsPath('C:/Windows/Fonts/smalle.fon'), WindowsPath('C:/Windows/Fonts/HARLOWSI.TTF'), WindowsPath('C:/Windows/Fonts/msjh.ttc'), WindowsPath('C:/Windows/Fonts/himalaya.ttf'), WindowsPath('C:/Windows/Fonts/TCMI____.TTF'), WindowsPath('C:/Windows/Fonts/ALGER.TTF'), WindowsPath('C:/Windows/Fonts/calibrib.ttf'), WindowsPath('C:/Windows/Fonts/verdanab.ttf'), WindowsPath('C:/Windows/Fonts/Deng.ttf'), WindowsPath('C:/Windows/Fonts/consolai.ttf'), WindowsPath('C:/Windows/Fonts/holomdl2.ttf'), WindowsPath('C:/Windows/Fonts/MSUIGHUB.TTF'), WindowsPath('C:/Windows/Fonts/BELLB.TTF'), WindowsPath('C:/Windows/Fonts/ROCKB.TTF'), WindowsPath('C:/Windows/Fonts/STENCIL.TTF'), WindowsPath('C:/Windows/Fonts/phagspab.ttf'), WindowsPath('C:/Windows/Fonts/MOD20.TTF'), WindowsPath('C:/Windows/Fonts/consolaz.ttf'), WindowsPath('C:/Windows/Fonts/PER_____.TTF'), WindowsPath('C:/Windows/Fonts/LFAXI.TTF'), WindowsPath('C:/Windows/Fonts/CALIFB.TTF'), WindowsPath('C:/Windows/Fonts/msyhl.ttc'), WindowsPath('C:/Windows/Fonts/72-Light.ttf'), WindowsPath('C:/Windows/Fonts/VIVALDII.TTF'), WindowsPath('C:/Windows/Fonts/PERI____.TTF'), WindowsPath('C:/Windows/Fonts/corbell.ttf'), WindowsPath('C:/Windows/Fonts/seguili.ttf'), WindowsPath('C:/Windows/Fonts/PERB____.TTF'), WindowsPath('C:/Windows/Fonts/BRLNSDB.TTF'), WindowsPath('C:/Windows/Fonts/trebuc.ttf'), WindowsPath('C:/Windows/Fonts/micross.ttf'), WindowsPath('C:/Windows/Fonts/coure.fon'), WindowsPath('C:/Windows/Fonts/seguibli.ttf'), WindowsPath('C:/Windows/Fonts/STHUPO.TTF'), WindowsPath('C:/Windows/Fonts/ROCKBI.TTF'), WindowsPath('C:/Windows/Fonts/Candarai.ttf'), WindowsPath('C:/Windows/Fonts/simhei.ttf'), WindowsPath('C:/Windows/Fonts/SAPGUI-Belize-Icons.TTF'), WindowsPath('C:/Windows/Fonts/LHANDW.TTF'), WindowsPath('C:/Windows/Fonts/PERTILI.TTF'), WindowsPath('C:/Windows/Fonts/COOPBL.TTF'), WindowsPath('C:/Windows/Fonts/STXINWEI.TTF'), WindowsPath('C:/Windows/Fonts/TCB_____.TTF'), WindowsPath('C:/Windows/Fonts/GLECB.TTF'), WindowsPath('C:/Windows/Fonts/WINGDNG2.TTF'), WindowsPath('C:/Windows/Fonts/PARCHM.TTF'), WindowsPath('C:/Windows/Fonts/NirmalaB.ttf'), WindowsPath('C:/Windows/Fonts/TEMPSITC.TTF'), WindowsPath('C:/Windows/Fonts/seguisb.ttf'), WindowsPath('C:/Windows/Fonts/constan.ttf'), WindowsPath('C:/Windows/Fonts/KUNSTLER.TTF'), WindowsPath('C:/Windows/Fonts/SitkaB.ttc'), WindowsPath('C:/Windows/Fonts/NIAGENG.TTF'), WindowsPath('C:/Windows/Fonts/ARLRDBD.TTF'), WindowsPath('C:/Windows/Fonts/Inkfree.ttf'), WindowsPath('C:/Windows/Fonts/monbaiti.ttf'), WindowsPath('C:/Windows/Fonts/GARABD.TTF'), WindowsPath('C:/Windows/Fonts/GILBI___.TTF'), WindowsPath('C:/Windows/Fonts/BOOKOSI.TTF'), WindowsPath('C:/Windows/Fonts/palab.ttf'), WindowsPath('C:/Windows/Fonts/BOD_BLAI.TTF'), WindowsPath('C:/Windows/Fonts/VINERITC.TTF')} matplotlib支持的樣式: ['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']畫圖結(jié)果:
2 設(shè)置2個坐標(biāo)
代碼:
#導(dǎo)入Matplotlib模塊 #from matplotlib import pyplot as plt import matplotlib.pyplot as plt import matplotlib.font_manager as FontManager import pandas as pd import numpy as np#導(dǎo)入數(shù)據(jù) xvalues1=[1,2,3,4,5] ysquares1=[x**2 for x in xvalues1] xvalues2=[1,2,3,4,5] ysquares2=[x+2 for x in xvalues2] xvalues3=[1,2,3,4,5] ysquares3=[x**2-2 for x in xvalues3] ysquares4=[x**2+2 for x in xvalues3]#設(shè)置圖表風(fēng)格,需要在畫圖前設(shè)置好,否則無效 plt.style.use('_mpl-gallery') #plt.rcParams['font.sans-serif']=['Times New Roman'] # 用來正常顯示中文標(biāo)簽 plt.rcParams['axes.unicode_minus']=False # 用來正常顯示負(fù)號 plt.rcParams["font.family"] = "Times New Roman"#設(shè)置圖中所有的字體#fig表示整張圖篇,ax表示圖片中的對應(yīng)圖表 fig1,ax_2=plt.subplots(1,1,figsize=(5, 2.7), layout='constrained')#一個圖中畫上下一個表'''#在圖表ax中畫圖''' al1,=ax_2.plot(xvalues1,ysquares1,color='black',linestyle='solid',marker='o',label='Square',linewidth=3) #設(shè)置ax_2的第二個坐標(biāo) ax_2_1=ax_2.twinx() # 在第二個坐標(biāo)上畫圖 al2,=ax_2_1.plot(xvalues2,ysquares2,color='red',linestyle='dashdot',marker= 'x',label='line',linewidth=1) #顯示legend ax_2_1.legend([al1,al2],['Square',"Line"])#顯示前面添加的標(biāo)簽label,可以傳遞[al1,al2],['Square',"Line"] #設(shè)置y軸范圍 ax_2.set_ylim(0, 40); ax_2_1.set_ylim(0, 10); #顯示圖表 plt.show()運(yùn)行結(jié)果:
?3 畫圖函數(shù)模塊化
代碼:
#導(dǎo)入Matplotlib模塊 import matplotlib.pyplot as pltdef plot_single_fig(ax,datax1,datay1,param_dict):'''在圖表ax中畫圖'''out = ax.plot(datax1, datay1,**param_dict)return out'''定義畫圖函數(shù),傳遞一組或兩組數(shù)據(jù)''' def plot_test(datax1,datay1,datax2=[],datay2=[],legend1="",legend2=""):#設(shè)置圖表風(fēng)格,需要在畫圖前設(shè)置好,否則無效plt.style.use('_mpl-gallery')#plt.rcParams['font.sans-serif']=['Times New Roman'] # 用來正常顯示中文標(biāo)簽plt.rcParams['axes.unicode_minus']=False # 用來正常顯示負(fù)號plt.rcParams["font.family"] = "Times New Roman"#設(shè)置圖中所有的字體#fig表示整張圖篇,ax表示圖片中的對應(yīng)圖表fig1,(ax1,ax2)=plt.subplots(2,1,figsize=(5, 2.7), layout='constrained')al1=plot_single_fig(ax1,datax1,datay1,{'linestyle': 'dashdot','marker': 'o','color': 'black','label': legend1,'linewidth': 3})al2=plot_single_fig(ax2, datax2, datay2,{'linestyle': 'dashdot','marker': '+', 'color': 'red', 'label': legend2, 'linewidth': 1})#顯示2個legendax1.legend() # 顯示前面添加的標(biāo)簽label,可以傳遞[al1,al2],['Square',"Line"]ax2.legend() # 顯示前面添加的標(biāo)簽label,可以傳遞[al1,al2],['Square',"Line"]#顯示圖表plt.show()'''調(diào)用函數(shù)畫圖''' #導(dǎo)入數(shù)據(jù) xvalues1=[1,2,3,4,5] ysquares1=[x**2 for x in xvalues1] xvalues2=[1,2,3,4,5] ysquares2=[x+2 for x in xvalues2]#調(diào)用畫圖函數(shù) plot_test(xvalues1,ysquares1,xvalues2,ysquares2,legend1='Square',legend2="Line")?運(yùn)行結(jié)果:
4 一張圖中多個坐標(biāo)系繪圖
代碼:
#導(dǎo)入Matplotlib模塊 import matplotlib.pyplot as plt#導(dǎo)入數(shù)據(jù) xvalues1=[1,2,3,4,5] ysquares1=[x**2 for x in xvalues1] xvalues2=[1,2,3,4,5] ysquares2=[x+2 for x in xvalues2] xvalues3=[1,2,3,4,5] ysquares3=[x**2-2 for x in xvalues3] ysquares4=[x**2+2 for x in xvalues3]'''通過subplot_mosaic繪制多個圖和坐標(biāo)''' fig, axd = plt.subplot_mosaic([['upleft', 'right'],['lowleft', 'right']], layout='constrained') axd['upleft'].set_title('upleft') axd['lowleft'].set_title('lowleft') axd['right'].set_title('right')axd['upleft'].plot(xvalues1, ysquares1,marker='o') axd['lowleft'].plot(xvalues2, ysquares2,marker='+') axd['right'].plot(xvalues3, ysquares3,marker='P')# 顯示圖表 plt.show()結(jié)果:
?5 設(shè)置坐標(biāo)刻度間隔MultipleLocator
代碼:
#導(dǎo)入Matplotlib模塊 import matplotlib.pyplot as plt #此類用于設(shè)置刻度間隔 from matplotlib.pyplot import MultipleLocator#導(dǎo)入數(shù)據(jù) xvalues1=[1,2,3,4,5] ysquares1=[x**2 for x in xvalues1] xvalues2=[1,2,3,4,5] ysquares2=[x+2 for x in xvalues2] xvalues3=[1,2,3,4,5] ysquares3=[x**2-2 for x in xvalues3]#設(shè)置X和Y軸的刻度間隔 x_major_locator=MultipleLocator(0.4) y_major_locator=MultipleLocator(4) #創(chuàng)建兩條坐標(biāo)軸的實(shí)例ax ax=plt.gca() #設(shè)置XY坐標(biāo)走的刻度間隔倍數(shù) ax.xaxis.set_major_locator(x_major_locator) ax.yaxis.set_major_locator(y_major_locator)'''#畫圖''' ax.plot(xvalues1,ysquares1,color='black',linestyle='solid',marker='o',label='Square',linewidth=3) ax.plot(xvalues2,ysquares2,color='red',linestyle='dashdot',marker= 'x',label='line',linewidth=1) ax.legend()#顯示前面添加的標(biāo)簽label,可以傳遞[al1,al2],['Square',"Line"]#顯示圖表 plt.show()執(zhí)行結(jié)果:可以看到x軸的間隔設(shè)置成了0.4,y軸間隔設(shè)置成了4
?6 一副圖畫3個表
代碼:
#導(dǎo)入Matplotlib模塊 import matplotlib.pyplot as plt#導(dǎo)入數(shù)據(jù) xvalues1=[1,2,3,4,5] ysquares1=[x**2 for x in xvalues1] xvalues2=[1,2,3,4,5] ysquares2=[-3,0,3,4,9] xvalues3=[1,2,3,4,5] ysquares3=[x**1.5 for x in xvalues3]plt.subplot(311,title="eg. three grafs in one photo") plt.plot(xvalues1,ysquares1) plt.ylabel('ysquares1') plt.grid(True) plt.gca().xaxis.grid(True, which='minor') # minor grid on tooplt.subplot(312) plt.plot(xvalues2,ysquares2) plt.ylabel('ysquares2') plt.grid(True) plt.gca().xaxis.grid(True, which='minor') # minor grid on tooplt.subplot(313) plt.plot(xvalues3,ysquares3) plt.ylabel('ysquares3') plt.grid(True) plt.gca().xaxis.grid(True, which='minor') # minor grid on tooplt.tight_layout() plt.show()結(jié)果:
附錄:
rcParams中的rc是什么意思: run configuration 運(yùn)行配置
事實(shí)上,pyplot使用rcParams來自定義圖形的各種默認(rèn)屬性,稱之為運(yùn)行配置參數(shù)。通過更改參數(shù)可以修改默認(rèn)的屬性,包括窗體大小、每英寸的點(diǎn)數(shù)、線條寬度、顏色、樣式、坐標(biāo)軸、坐標(biāo)和網(wǎng)絡(luò)屬性、文本、字體等。
| 序號 | 屬性 | 說明 | 
| 1 | plt.rcParams[’axes.unicode_minus’] = False | 字符顯示 | 
| 2 | plt.rcParams[’font.sans-serif’] = ‘SimHei’ | 設(shè)置字體 | 
| ? | 線條樣式:lines | ? | 
| 3 | plt.rcParams[’lines.linestyle’] = ‘-.’ | 線條樣式 | 
| 4 | plt.rcParams[’lines.linewidth’] = 3 | 線條寬度 | 
| 5 | plt.rcParams[’lines.color’] = ‘blue’ | 線條顏色 | 
| 6 | plt.rcParams[’lines.marker’] = None | 默認(rèn)標(biāo)記 | 
| 7 | plt.rcParams[’lines.markersize’] = 6 | 標(biāo)記大小 | 
| 8 | plt.rcParams[’lines.markeredgewidth’] = 0.5 | 標(biāo)記附近的線寬 | 
| ? | 橫、縱軸:xtick、ytick | ? | 
| 9 | plt.rcParams[’xtick.labelsize’] | 橫軸字體大小 | 
| 10 | plt.rcParams[’ytick.labelsize’] | 縱軸字體大小 | 
| 11 | plt.rcParams[’xtick.major.size’] | x軸最大刻度 | 
| 12 | plt.rcParams[’ytick.major.size’] | y軸最大刻度 | 
| ? | figure中的子圖:axes | ? | 
| 13 | plt.rcParams[’axes.titlesize’] | 子圖的標(biāo)題大小 | 
| 14 | plt.rcParams[’axes.labelsize’] | 子圖的標(biāo)簽大小 | 
| ? | 圖像、圖片:figure、savefig | ? | 
| 15 | plt.rcParams[’figure.dpi’] | 圖像分辨率 | 
| 16 | plt.rcParams[’figure.figsize’] | 圖像顯示大小 | 
| 17 | plt.rcParams[’savefig.dpi’] | 圖片像素 | 
總結(jié)
以上是生活随笔為你收集整理的Python-matplotlib用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: overleaf 插入图片_latex中
- 下一篇: c语言三个数从小到大排序/输出
