matplotlib从入门到精通教程_第1页
matplotlib从入门到精通教程_第2页
matplotlib从入门到精通教程_第3页
matplotlib从入门到精通教程_第4页
matplotlib从入门到精通教程_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、、导入1.导入matplotlib库简写为pitimport matpl otiib .pyplot as p It二、基本图表2.用plot方法画出x=(0,10)间sin的图像x = np.linsp ace(0, 10, 30)p It .pl ot(x, np.sin( x);3.用点加线的方式画出x=(0,10)间sin的图像间sin的点图像P It .pl ot(x, np.sin( x), -o);4.用 scatter 方法画出 x=(0,10)p lt.scatter(x, np .sin(x);5.用饼图的面积及颜色展示一组4维数据rng = np .ra ndom.Ra

2、ndomState(0)x = rn g.ra ndn (100)y = rn g.ra ndn (100)colors = rn g.ra nd(100)sizes = 1000 * rng.ra nd(100)p lt.scatter(x, y, c=colors, s=sizes, alp ha=0.3, cma p=viridis)plt.colorbar(); # 展示色阶6.绘制一组误差为 0.8的数据的误差条图x = np.linsp ace(0, 10, 50)dy = 0.8y = np.sin(x) + dy * np .ra ndom .randn(50)P lt.eiT

3、orbar(x, y, yerr=dy, fmt=.k)7.绘制一个柱状图 x = 123,4,5,6,7,8y = 3,1,4,5,8,9,7,2label=A,B,C,D,E,F,G,HP lt.bar(x,y,tick_label = label);8. 绘制一个水平方向柱状图P lt.barh(x,y,tick_label = label);9. 绘制1000个随机值的直方图data = np.ran dom.ra ndn (1000) plt.hist(data);10. 设置直方图分30个bins,并设置为频率分布p lt.hist(data, bin s=30,histt yp

4、e=ste pfilled, den sity=True) p lt.show();11. 在一张图中绘制3组不同的直方图,并设置透明度x1 = np.ran dom. normal 0.8, 1000) x2 = np.ran dom. normal(-2, 1, 1000) x3 = np.ran dom. normal(3, 2, 1000) kwargs = dict(a Ip ha=0.3, bin s=40, den sity = True) plt.hist(x1, *kwargs);plt.hist(x2, *kwargs);plt.hist(x3, *kwargs);12.

5、绘制一张二维直方图mean = 0, 0cov = 1, 1, 1,2x, y = np.ran dom.multivariate_ no rmal(mea n, cov, 10000).T plt.hist2d(x, y, bin s=30);13. 绘制一张设置网格大小为 30的六角形直方图P lt.hexb in(x, y, gridsize=30);二、自定义图表兀素14. 绘制x=(0,10)间sin的图像,设置线性为虚线x = np .lins pace(0,10,100)plt. plot(x,n p.sin(x),-);15设置y轴显示范围为(-1.5,1.5)x = np .

6、lins pace(0,10,100) pl t. pl ot(x, np .sin(x) plt.ylim(-1.5, 1.5);16.设置 x,y 轴标签 variable x, value yx = np .lins pace(0.05, 10, 100)y = np .sin(x)pit. pl ot(x, y, label=sin(x) p It.xlabelCvariable x);plt.ylabel(value y);17.设置图表标题三角函数”x = np .lins pace(0.05, 10, 100) y = np .sin(x) plt. pl ot(x, y, la

7、bel=sin(x) plt.title(三角函数);18. 显示网格x = np .lins pace(0.05, 10, 100) y = np .sin(x) plt .pl ot(x, y) plt.gridO19. 绘制平行于x轴y=0.8的水平参考线x = np .lins pace(0.05, 10, 100) y = np .sin(x) plt .pl ot(x, y) p lt.axhline(y=0.8, ls=-, c=r)20. 绘制垂直于x轴x6的参考区域,以及y轴yv0.2 and y-0.2的参考区x = np .lins pace(0.05, 10, 100)

8、 y = np .sin(x) plt .pl ot(x, y)plt.axvspan(xmin=4, xmax=6, facecolor=r, alpha=0.3) # 垂直 x 轴plt.axhspan(ymin=-0.2, ymax=0.2, facecolor=y, alpha=0.3);# 垂直 y 轴21. 添加注释文字sin(x)x = np .lins pace(0.05, 10, 100) y = np .sin(x) plt .pl ot(x, y) p lt.text(3.2, 0, sin(x), weight=bold, color=r);22. 用箭头标出第一个峰值

9、x = np .lins pace(0.05, 10, 100)y = np .sin(x) plt .pl ot(x, y)p lt.annotate(maximum,xy=(n p.pi/2, 1),xytext=(np.p i/2+1, 1), weight-bold.color=r,arrow prop s=dict(arrowstyIe=-, connectionstyle=arc3, color=r);四、自定义图例23.在一张图里绘制 sin,cos的图形,并展示图例 x = np.linsp ace(0, 10, 1000) fig, ax = p lt.sub pl ots(

10、) ax.pl ot(x, np.sin( x), label=s in)ax.pl ot(x, np .cos(x), -, label=cos)ax.l ege nd();24.调整图例在左上角展示,且不显示边框 ax.l ege nd(loc= upper left, frame on=False); fig25.调整图例在画面下方居中展示,且分成2列 ax.l ege nd(frame on=False, loc=lower cen ter, n col=2) fig26.绘制的图像,并只显示前2者的图例 y = np.sin( x:, np.n ewaxis + np.pi * np

11、.aran ge(0, 2, 0.5) lines = p It .pl ot(x, y) # lines是plt.Line2D 类型的实例的列表 plt.lege nd(li nes:2, first, seco nd);#第二个方法 #p It. plot(x, y:, 0, label=first)#p It. plot(x, y:, 1, label=seco nd)#plt.pl ot(x, y:, 2:)#p It .l ege nd(framea Ip ha=1, frame on=True);27.将图例分不同的区域展示 fig, ax = p lt.sub pl ots()

12、lines =styles =-, -, -., :x = np.linsp ace(0, 10, 1000) for i in ran ge(4):lines += ax.pl ot(x, np.sin(x - i * np.pi / 2),stylesi, color=black)ax.axis(equal)#设置第一组标签ax.lege nd(lin es:2, li ne A, li ne B,loc= upper right, frame on=False)#创建第二组标签from mat pl otiib.lege nd import Lege ndleg = Lege nd(ax

13、, li nes2:, li ne C, li ne D, loc=lower right, frame on=False) ax.add_artist(leg);五、自定义色阶28.展示色阶x = np.linsp ace(0, 10, 1000)I = np.sin(x) * np. cos(x:, np.n ewaxis) p It.imshow(l) p lt.colorbar();29.改变配色为gray p lt.imshow(l, cma p=gray);30.将色阶分成6个离散值显示 p lt.imshow(l, cmap=p lt.cm.get_cma p(Blues, 6)

14、 p lt.colorbar()plt.clim(-1, 1);六、多子图31.在一个1010的画布中,(0.65,0.65)的位置创建一个0.20.2的子图ax1 = P lt.axesOax2 = plt.axes(0.65, 0.65, 0.2, 0.2)32.在2个子图中,显示sin(x)和cos(x)的图像 fig = p It.figureOax1 = fig.add_axes(0.1, 0.5, 0.8, 0.4, ylim=(-1.2, 1.2)ax2 = fig.add_axes(0.1, 0.1,0.8, 0.4, ylim=(-1.2, 1.2) x = np.linsp

15、 ace(0, 10) ax1. plot(np.sin( x);ax2. plot(np .cos(x);33.用for创建6个子图,并且在图中标识出对应的子图坐标for i in ran ge(1,7):plt.sub plot(2, 3, i)p lt.text(0.5, 0.5, str(2, 3, i),fo ntsize=18, ha=ce nter) #方法二# fig - p It.figureO# fig.sub pl ots_adjust(hs pace=0.4, wsp ace=0.4)# for i in ran ge(1,7):# ax - fig.add_sub p

16、lot(2, 3, i)# ax.text(0.5, 0.5, str(2, 3, i),fo ntsize=18, ha=ce nter)34.设置相同行和列共享x,y轴 fig, ax = p lt.sub pl ots(2, 3, sharex=col, sharey=row)35.用的方式取出每个子图,并添加子图座标文字for i in ran ge(2):for j in ran ge(3):axi, j.text(0.5, 0.5, str(i, j),fo ntsize=18, ha-ce nter)fig36. 组合绘制大小不同的子图,样式如下Image Namegrid -

17、plt.GridS pec(2, 3, ws pace=0.4, hsp ace=0.3) plt.subplot(grid0, 0)plt.sub plot(grid0, 1:)plt.subplot(grid1, :2)plt.sub plot(grid1,2);37. 显示一组二维数据的频度分布,并分别在x,y轴上,显示该维度的数据的频度分布 mean = 0, 0cov = 1, 1, 1,2x, y = np.ran dom.multivariate_ no rmal(mea n, cov, 3000).T # Set up the axes with grids pecfig =

18、p lt.figure(figsize=(6, 6)grid = p It.GridS pec(4, 4, hsp ace=0.2, wsp ace=0.2)main_ax = fig.add_sub pl ot(gnd:-1, 1:)y_hist = fig.add_sub pl ot(gnd:-1,0, xticklabels=, sharey=ma in_ax) x_hist = fig.add_sub pl ot(gnd-1, 1:, yticklabels=, sharex=ma in_ax) # scatter points on the main axes main _ax.sc

19、atter(x, y,s=3,a Ip ha=0.2) # histogram on the attached axes x_hist.hist(x, 40, histty pe=ste pfilled, orie ntati on= vertical) x_hist.i nvert_yaxis() y_hist.hist(y, 40, histty pe=ste pfilled,orie ntati on=horiz on tal) y_hist.i nvert_xaxis()七、三维图像38. 创建一个三维画布 from mp l_toolkits import mpl ot3dfig =

20、 p It.figureOax = p lt.axes( projecti on=3d)39. 绘制一个三维螺旋线 ax = P lt.axes( projecti on=3d)# Data for a three-dime nsion al li ne zli ne = np .li nsp ace(0, 15, 1000) xli ne = np .si n( zli ne)yli ne = np .cos(zli ne)ax.pl ot3D(xli ne, yli ne, zli ne);40. 绘制一组三维点 ax = p lt.axes( projecti on=3d)zdata =

21、 15 * np.ran dom.ra ndom(100)xdata = np.sin( zdata) + 0.1 * np .ra ndom .randn (100)ydata = np. cos(zdata) + 0.1 * np.ran dom.ra ndn (100) ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap=Gree ns);八、宝可梦数据集可视化41.展示前5个宝可梦的Defense,Attack,HP的堆积条形图 P okem on = dfName:5hp = dfHP :5attack = dfAttack:5defe n

22、se = dfDefe nse:5ind = x for x, _ in enu merate( pokem on) plt.figure(figsize=(10,10)p lt.bar(i nd, defe nse, width=0.8, label=Defe nse, color=blue, bottom=attack+h p)p lt.bar(i nd, attack, width=0.8, label=Attack, color=gold, bottom=h p)plt.bar(i nd, h p, width=0.8, label=H p, color=red) p lt.xtick

23、s(i nd, p okem on)plt.ylabel(Value)plt.xlabel( Pokemo n)p It .l ege nd(loc=upper right)plt.title(5 P okemon Defe nse & Attack & Hp)P lt.show()42.展示前5个宝可梦的Attack,HP的簇状条形图N = 5 pokemon_h p = dfHP :5 pokemon_attack = dfAttack:5 ind = np.aran ge(N)width = 0.35p lt.bar(i nd, p okem on_hp, width, label=HP

24、)p lt.bar(i nd + width, p okem on _attack, width,label=Attack) plt.ylabel(Values)plt.title( P okemon Hp & Attack) plt.xticks(i nd + width / 2, (dfName:5),rotatio n=45) p It .l ege nd(loc=best)p lt.show() 43.展示前5个宝可梦的Defense,Attack,HP的堆积图x = dfName:4y1 = df HP :4y2 = dfAttack:4y3 = dfDefe nse:4 label

25、s = HP , Attack, Defe nse fig, ax = p lt.sub pl ots()ax.stack plot(x, y1, y2, y3)ax.l ege nd(loc=upper left, labels=labels) plt.xticks(rotatio n=90)p lt.show() 44.公用x轴,展示前5个宝可梦的 Defense,Attack,HP的折线图 x = dfName:5y1 = dfH P:5y2 = dfAttack:5y3 = dfDefe nse:5 # Create two sub pl ots shari ng y axisfig,

26、 (ax1, ax2,ax3) = p lt.sub pl ots(3, sharey=True) ax1. plot(x, y1, ko-)ax1.set(title=3 sub pl ots, ylabel=H P) ax2. plot(x, y2, r.-)ax2.set(xlabel= Pokemo n, ylabel=Attack)ax3. plot(x, y3,:)ax3.set(xlabel=P okem on, ylabel=Defe nse) p lt.show() 45.展示前15个宝可梦的Attack,HP的折线图 plt. ploMdfTH P :15, -r,label= HP)plt .p lot(dfAttack:15, :g,label=Attack)p lt.lege nd();46.用scatter的x,y,c属性,展示所有宝可梦的Defense,Attack,HP数据x = dfAttack y = dfDefe nse colors = df HP p lt.scatter(x, y, c=colors, alp ha=0

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论