Python-matplotlib用法

安装matplotlib:

        pip install matplotlib

help doc:

Basic Usage — Matplotlib 3.5.1 documentationhttps://matplotlib.org/stable/tutorials/introductory/usage.html推荐文章:

        字体:【Python基础】matplotlib字体设置看这一篇就够了_fengdu78的博客-CSDN博客

        绘图:Python绘图总结(Matplotlib篇)之字体、文本及注释_wuzlun的专栏-CSDN博客_matplotlib 字体

1 基础绘图

直接上代码:

(1)代码中打印出了一些默认的格式或样式

(2)代码中进行了样式的设置示例

#导入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

#打印图标样式,看看自带的样式有哪些
print(f"matplotlib支持的默认格式:\n{plt.style.available}")
print(f"windows系统支持的字体:\n{FontManager._get_win32_installed_fonts()}")
print(f"matplotlib支持的样式:\n{plt.style.available}")
for font in FontManager.fontManager.ttflist:
    # 查看字体名以及对应的字体文件名
    print(font.name, '-', font.fname)

#导入数据
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]

#设置图表风格,需要在画图前设置好,否则无效
plt.style.use('_mpl-gallery')
#plt.rcParams['font.sans-serif']=['Times New Roman'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
plt.rcParams["font.family"] = "Times New Roman"#设置图中所有的字体

#fig表示整张图篇,ax表示图片中的对应图表,如果是两张图则实例是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()#显示前面添加的标签label,可以传递[al1,al2],['Square',"Line"]

#设置图表显示的标签和坐标轴
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])#设置坐标轴范围也可以用ax.set_ylim(0, 40);
ax[0].tick_params(axis='both',which='major',labelsize=8)#设置刻度字体大小 

#以x轴为横坐标画出数据的分布
n, bins, patches = ax[0].hist(xvalues1, 50, density=1, facecolor='C0', alpha=0.75)

'''ax[1]中画散点图,S设置尺寸'''
ax[1].set_title("Simple scatter",fontsize=20,color='red')
#颜色设置可以采用RGB的形式(0, 0.8, 0),值越接近 0,指定的颜色越深;值越接近 1,指定的颜色越浅
ax[1].scatter(xvalues3, ysquares3, s=100, facecolor=(0, 0.8, 0), edgecolor='blue');#画散点图
#颜色映射(colormap)是一系列颜色,从起始颜色渐变到结束颜色,此处通过Y轴的坐标值进行映射
ax[1].scatter(xvalues3, ysquares4, s=200, c=ysquares4,cmap=plt.cm.Blues);
#对特定的点进行注释,xy=箭头的坐标,xytext=文字的坐标
ax[1].annotate('annotate test', xy=(3, 10), xytext=(3.5, 20),arrowprops=dict(facecolor='black', shrink=0.05))

'''在图中打印一行字(1.5,15.5)为文字左边的坐标(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裁剪空白区域
plt.savefig('Simple_plot.png', bbox_inches='tight') 

#显示图表
plt.show()

打印的默认格式和样式,可以看到画图支持的样式和支持的字体:

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']
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']

画图结果:

2 设置2个坐标

代码:

#导入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

#导入数据
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]

#设置图表风格,需要在画图前设置好,否则无效
plt.style.use('_mpl-gallery')
#plt.rcParams['font.sans-serif']=['Times New Roman'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
plt.rcParams["font.family"] = "Times New Roman"#设置图中所有的字体

#fig表示整张图篇,ax表示图片中的对应图表
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)
#设置ax_2的第二个坐标
ax_2_1=ax_2.twinx()
# 在第二个坐标上画图
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"])#显示前面添加的标签label,可以传递[al1,al2],['Square',"Line"]
#设置y轴范围
ax_2.set_ylim(0, 40);
ax_2_1.set_ylim(0, 10);
#显示图表
plt.show()

运行结果:

 3 画图函数模块化

代码:

#导入Matplotlib模块
import matplotlib.pyplot as plt

def plot_single_fig(ax,datax1,datay1,param_dict):
    '''在图表ax中画图'''
    out = ax.plot(datax1, datay1,**param_dict)
    return out


'''定义画图函数,传递一组或两组数据'''
def plot_test(datax1,datay1,datax2=[],datay2=[],legend1="",legend2=""):
    #设置图表风格,需要在画图前设置好,否则无效
    plt.style.use('_mpl-gallery')
    #plt.rcParams['font.sans-serif']=['Times New Roman'] # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
    plt.rcParams["font.family"] = "Times New Roman"#设置图中所有的字体

    #fig表示整张图篇,ax表示图片中的对应图表
    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个legend
    ax1.legend()  # 显示前面添加的标签label,可以传递[al1,al2],['Square',"Line"]
    ax2.legend()  # 显示前面添加的标签label,可以传递[al1,al2],['Square',"Line"]

    #显示图表
    plt.show()

'''调用函数画图'''
#导入数据
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]

#调用画图函数
plot_test(xvalues1,ysquares1,xvalues2,ysquares2,legend1='Square',legend2="Line")

 运行结果:

4 一张图中多个坐标系绘图

代码:

#导入Matplotlib模块
import matplotlib.pyplot as plt

#导入数据
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绘制多个图和坐标'''
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()

结果:

 5 设置坐标刻度间隔MultipleLocator

代码:

#导入Matplotlib模块
import matplotlib.pyplot as plt
#此类用于设置刻度间隔
from matplotlib.pyplot import MultipleLocator

#导入数据
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]

#设置X和Y轴的刻度间隔
x_major_locator=MultipleLocator(0.4)
y_major_locator=MultipleLocator(4)
#创建两条坐标轴的实例ax
ax=plt.gca()
#设置XY坐标走的刻度间隔倍数
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()#显示前面添加的标签label,可以传递[al1,al2],['Square',"Line"]

#显示图表
plt.show()

执行结果:可以看到x轴的间隔设置成了0.4,y轴间隔设置成了4

 6 一副图画3个表

代码:

#导入Matplotlib模块
import matplotlib.pyplot as plt

#导入数据
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 too

plt.subplot(312)
plt.plot(xvalues2,ysquares2)
plt.ylabel('ysquares2')
plt.grid(True)
plt.gca().xaxis.grid(True, which='minor')  # minor grid on too

plt.subplot(313)
plt.plot(xvalues3,ysquares3)
plt.ylabel('ysquares3')
plt.grid(True)
plt.gca().xaxis.grid(True, which='minor')  # minor grid on too

plt.tight_layout()
plt.show()

结果:

附录:

rcParams中的rc是什么意思: run configuration 运行配置

事实上,pyplot使用rcParams来自定义图形的各种默认属性,称之为运行配置参数。通过更改参数可以修改默认的属性,包括窗体大小、每英寸的点数、线条宽度、颜色、样式、坐标轴、坐标和网络属性、文本、字体等。

序号属性说明
1plt.rcParams[’axes.unicode_minus’] = False字符显示
2plt.rcParams[’font.sans-serif’] = ‘SimHei’设置字体
 线条样式:lines 
3plt.rcParams[’lines.linestyle’] = ‘-.’线条样式
4plt.rcParams[’lines.linewidth’] = 3线条宽度
5plt.rcParams[’lines.color’] = ‘blue’线条颜色
6plt.rcParams[’lines.marker’] = None默认标记
7plt.rcParams[’lines.markersize’] = 6标记大小
8plt.rcParams[’lines.markeredgewidth’] = 0.5标记附近的线宽
 横、纵轴:xtick、ytick 
9plt.rcParams[’xtick.labelsize’]横轴字体大小
10plt.rcParams[’ytick.labelsize’]纵轴字体大小
11plt.rcParams[’xtick.major.size’]x轴最大刻度
12plt.rcParams[’ytick.major.size’]y轴最大刻度
 figure中的子图:axes 
13plt.rcParams[’axes.titlesize’]子图的标题大小
14plt.rcParams[’axes.labelsize’]子图的标签大小
 图像、图片:figure、savefig 
15plt.rcParams[’figure.dpi’]图像分辨率
16plt.rcParams[’figure.figsize’]图像显示大小
17plt.rcParams[’savefig.dpi’]图片像素

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值