提到Python的可视化,那一定无法忽视matplotlib库的存在。对于初学者来说,掌握它就已经足够应付大多数场合的使用了。并且一些针对气象绘图出现的库如metpy、psyplot等基本都需要结合matplotlib库使用,可以说它在Python绘图领域一枝独秀,那么今天就来了解一下它的基本操作吧!
1.Matplotlib绘图结构
上述是matplotlib官网给出的解释其绘图结构的图,对照上图可以大致得到想要改动部分的函数,实际应用中,如果想要更改某一部分的设置,就可以对照上图查找具体更改的是哪一部分内容,再对照改动。例如想更改图例,可以发现图例部分上图用legend表示了出来,那么就可以在官方文档下查找legend的使用方法。在那么,实际画图过程中的具体流程是怎样的呢?
2.绘图基本流程
与之前介绍过的NCL、Matlab等软件类似,其画图逻辑也是基于Figure和Axes,即Figure是创建的准备用来进行绘图的画布,而Axes可以用来设置画图时,真正需要用到的画布上的区域,每个Figure通常包含至少一个Axes。绘图基本流程大致为:
A.创建Figure画布
B.创建子图Axes对象
C.调用相应的函数绘制图形
D.设置图例legend、x/y坐标轴、网格grid
E.设置标题title、文本text、注释annotate
F. plt.savefig()保存图形
G.plt.show()显示图形,绘制结束
用一段官网上的代码来理解:
#导入库import matplotlibimport matplotlib.pyplot as pltimport numpy as np# 准备绘图数据t = np.arange(0.0, 2.0, 0.01)s = 1 + np.sin(2 * np.pi * t)#设置绘图区域fig, ax = plt.subplots()#绘制折线图ax.plot(t, s)#设置网格ax.grid()#设置坐标轴标签和标题ax.set(xlabel='time (s)', ylabel='voltage (mV)', title='About as simple as it gets, folks')#保存图形fig.savefig("test.png")#显示图像plt.show()
关于图上属性的常用设置函数可以参考以下:
ax.set_xlabel('xname') #设置x轴名称ax.set_ylabel('yname') #设置y轴名称ax.set_title('title') #设置标题ax.legend() #设置图例:ax.set_xlim(0,5) #设置x轴范围ax.set_ylim(0,5) #设置y轴范围ax.set_xticks((0, 2, 4, 6)) #设置x轴刻度ax.set_xticks((0, 2, 4, 6)) #设置y轴刻度ax.set_xticklabels(labels=['x1','x2','x3','x4'],rotation=30,fontsize='small') #设置x刻度的显示文本ax.set_yticklabels() #设置y刻度的显示文本,同上ax.text() #指定位置显示文本ax.annotate() #添加标注,参数:注释文本、指向点、文字位置、箭头属性ax.grid() #显示网格ax.spines() #画出显示数据区域边界的线
3.基本绘图函数
上一部分了解了什么是图像属性的设置,那么步骤C中绘制图像的具体函数有哪些呢?
matplotlib函数库提供了丰富的绘图函数可供选择,具体可查看其官网示例:
https://matplotlib.org/gallery/index.html
点击网页右侧的链接即可直达你想要的图形绘制实例。下表是matplotlib绘图函数一览:
函数 | 功能 |
plt.plot(x,y,fmt) | 坐标系 |
plt.boxplot(data,notch,position) | 箱型图 |
plt.bar(left,height,width,bottom) | 柱状图 |
plt.barh(width,bottom,left,height) | 横向柱状图 |
plt.polar(theta,r) | 极坐标系 |
plt.pie(data,explode) | 饼图 |
plt.psd(x,NFFT=256,pad_to,Fs) | 功率谱 密度图 |
plt.specgram(x,NFFT=256,pad_to,F) | 谱图 |
plt.cohere(x,y,NFFT=256,Fs) | X-Y相关性函数 |
plt.scatter(x,y) | 散点图 |
plt.step(x,y,where) | 步阶图 |
plt.hist(x,bins,normed) | 直方图 |
plt.contour(X,Y,Z,N) | 等值图 |
plt.vlines() | 垂直图 |
plt.stem(x,y,linefmt,markerfmt) | 柴火图 |
plt.plot_date() | 数据日期图 |
基本介绍就到这里,练手的实例和各种高阶玩法请上家园来~
ps:微信不能打开外部链接,烦请各位复制链接至浏览器食用~
Python气象方面的扩展包:by:frinly
bbs.06climate.com/forum.php?mod=viewthread&tid=32981
Metpy绘图例程系列(中文翻译版):by:qazwsxpy
bbs.06climate.com/forum.php?mod=viewthread&tid=96820
pyngl绘图命令解析:by:喵星观天
bbs.06climate.com/forum.php?mod=viewthread&tid=8909
分享一个Python绘图例子的网站(气象绘图),建议有兴趣者学习:by:Swallow
bbs.06climate.com/forum.php?mod=viewthread&tid=52980
Python可视化库matplotlib与IDL对比:by:15195775117
bbs.06climate.com/forum.php?mod=viewthread&tid=91385
我的python学习第二步:绘制基本图形,查看某个模块是否存在?Basemapde 基本使用:by:Swallow
bbs.06climate.com/forum.php?mod=viewthread&tid=47361
利用Python画中国地图外加站点分布:by:xiuyuanyang
bbs.06climate.com/forum.php?mod=viewthread&tid=51427
Python学习记录(从小白开始)——Python画站点图:by:辞觉
bbs.06climate.com/forum.php?mod=viewthread&tid=88233
Python 气象绘图(读取文件数据;输出路径 按照需求修改):by:YanPenG
bbs.06climate.com/forum.php?mod=viewthread&tid=60499
Python读取txt文件并绘制站点降水散点图:by:平流层的萝卜
bbs.06climate.com/forum.php?mod=viewthread&tid=36334
Python读取全国自动站CMORPH数据并画图:by:Masterpiece
bbs.06climate.com/forum.php?mod=viewthread&tid=90192
绘制中国地图并标出各省省会:by:Jack_TEA
bbs.06climate.com/forum.php?mod=viewthread&tid=57248
用python画散点图,并在没给点上标注文本,设置坐标轴长度,字体等!:by:Jeaven
bbs.06climate.com/forum.php?mod=viewthread&tid=58658
pyton画青藏高原及要素散点图:by:chiqu296
bbs.06climate.com/forum.php?mod=viewthread&tid=57889
绘制850hPa风场和500hPa位势高度场:by:帅帅f
bbs.06climate.com/forum.php?mod=viewthread&tid=95783
python+cartopy绘制风矢及等高线:by:deerfcb
bbs.06climate.com/forum.php?mod=viewthread&tid=90892
通过merra2再分析资料制作高空高度场水汽垂直速度风场迭加图:by:王先生
bbs.06climate.com/forum.php?mod=viewthread&tid=89892
matplotlib+cartopy画WRF小时降水:by:风暴之灵
bbs.06climate.com/forum.php?mod=viewthread&tid=95537
Python学习记录-绘制最简单平面图:by:youxia1873
bbs.06climate.com/forum.php?mod=viewthread&tid=68367
利用Python绘制2019年夏季臭氧平均浓度(MDA8):by:倔强青铜
bbs.06climate.com/forum.php?mod=viewthread&tid=96053
python画风垂直切变填色图:by:huyy
bbs.06climate.com/forum.php?mod=viewthread&tid=67873
Python画任意两点间的地形剖面图:by:灭火器
bbs.06climate.com/forum.php?mod=viewthread&tid=97385
手把手系列-——Python绘制GFS天气图:by:又是那隻貓
bbs.06climate.com/forum.php?mod=viewthread&tid=52859
重磅推出,利用matplotlib和basemap绘制micaps数据:by:非对称
bbs.06climate.com/forum.php?mod=viewthread&tid=51944
Python读取nc, HDF数据,以及反演MODIS L1B 云顶亮温实例:by:沙颖凯
bbs.06climate.com/forum.php?mod=viewthread&tid=31766
分享Python读取NC文件并画阴影图的源代码:by:victory_19
bbs.06climate.com/forum.php?mod=viewthread&tid=90642
基于Metpy的Skew-T图绘制程序:by:eeeee
bbs.06climate.com/forum.php?mod=viewthread&tid=89775
分享一个污染玫瑰图的代码,提问极坐标轴更改方法:by:CynthiaLee
bbs.06climate.com/forum.php?mod=viewthread&tid=96235
使用Python绘制HYSPLIT后向轨迹:by:帅帅f
bbs.06climate.com/forum.php?mod=viewthread&tid=96694
风云4号AWX数据读取画图:by:yuyue1314
bbs.06climate.com/forum.php?mod=viewthread&tid=89296
Python FY2E卫星HDF5格式的TBB数据画图:by:chiqu296
bbs.06climate.com/forum.php?mod=viewthread&tid=56315
Python3.x 爬取历史探空数据并绘图:by:小其其格
bbs.06climate.com/forum.php?mod=viewthread&tid=89611
基于Siphon和MetPy获取探空数据并绘图:by:hzwjy
bbs.06climate.com/forum.php?mod=viewthread&tid=89894
Python快速绘图(三维图绘制):by:Hua_Zhang
bbs.06climate.com/forum.php?mod=viewthread&tid=94237
Python画南极地图-借花献佛:by:对方的凤飞飞
bbs.06climate.com/forum.php?mod=viewthread&tid=96290
用Python绘制森林火险气象等级预报图:by:sskkyy2008
bbs.06climate.com/forum.php?mod=viewthread&tid=96089
数据可视化的秘密:by:ztftom
bbs.06climate.com/forum.php?mod=viewthread&tid=12302
CartoPy画色斑图的时候无法同时显示地图和数据:by: QCD
bbs.06climate.com/forum.php?mod=viewthread&tid=66849
Python小技巧:绘制矩形框和零度经线断开的处理:by:不想去气象局
bbs.06climate.com/forum.php?mod=viewthread&tid=97707
关于绘制等经纬度netcdf格式雷达图回波区位置偏移的问题:by:樱桃小丸子
bbs.06climate.com/forum.php?mod=viewthread&tid=95465
[求助] Python画风玫瑰图的风向标签与教程不一致:
bbs.06climate.com/forum.php?mod=viewthread&tid=97560
[求助] Python如何绘制天气符号?
bbs.06climate.com/forum.php?mod=viewthread&tid=37579
[求助]Python源码在此,请问怎么把nc文件数据在可视化时将lev从底层到高层排列?源码分享:
bbs.06climate.com/forum.php?mod=viewthread&tid=89755
[求助],用cartopy画图,想画120E-0°范围的,应该怎么设置呢?
bbs.06climate.com/forum.php?mod=viewthread&tid=92138
[求助] Python 画剖面图,反转y轴:
bbs.06climate.com/forum.php?mod=viewthread&tid=50939
[求助] 关于nc数据绘制hgt图像:
bbs.06climate.com/forum.php?mod=viewthread&tid=89852
本期内容到此结束!意犹未尽?
欢迎到家园Python板块,一起讨论你的疑问吧!
http://bbs.06climate.com/
编辑:星星伴月