Matplotlib 入门(三):多图合并
来源:互联网 发布:提拉紧致面膜 知乎 编辑:程序博客网 时间:2024/06/02 03:36
一、多合一显示
1、subplot方法:设置行、列和起始点
plt.subplot(2,1,1) # 分成两行一列,起始点为1
2、代码
# -*- coding: utf-8 -*-"""Created on Sun Sep 24 15:02:51 2017@author: ryoyun"""# subplot 多合一显示import matplotlib.pyplot as pltplt.figure()plt.subplot(2,1,1) # 分成两行一列,起始点为1plt.plot([0,1],[0,1]) # 设置xy轴范围plt.subplot(2,3,4) # 分成两行三列,起始点位4plt.plot([0,1],[0,2])plt.subplot(2,3,5)plt.plot([0,1],[0,3])plt.subplot(2,3,6)plt.plot([0,1],[0,4])plt.show()
3、效果
二、分格显示
1、代码
# -*- coding: utf-8 -*-"""Created on Sun Sep 24 15:11:01 2017@author: ryoyun"""# 分格显示import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspec# method 1: subplot2grid#############################plt.figure()#ax1 = plt.subplot2grid((3,3),(0,0),colspan = 3,rowspan = 1)#ax1.plot([1,2],[1,2])#ax1.set_title('ax1_title')#plt.figure()#ax2 = plt.subplot2grid((3,3),(1,0),colspan = 2)#ax3 = plt.subplot2grid((3,3),(1,2),rowspan = 2)#ax4 = plt.subplot2grid((3,3),(2,0))#ax5 = plt.subplot2grid((3,3),(2,1))# method 2: gridspec############################plt.figure()#gs = gridspec.GridSpec(3,3)#ax1 = plt.subplot(gs[0,:])#ax2 = plt.subplot(gs[1,:2])#ax3 = plt.subplot(gs[1:,2])#ax4 = plt.subplot(gs[-1,0])#ax5 = plt.subplot(gs[-1,-2])# method 3: easy to define structure###########################f,((ax11,ax12),(ax21,ax22)) = plt.subplots(2,2,sharex=True,sharey=True)ax11.scatter([1,2],[1,2])plt.tick_layout()plt.show()
2、效果
三、图中图
1、每个图的位置设定很重要
left,bottom,width,height = 0.1,0.1,0.8,0.8
ax1 = fig.add_axes([left,bottom,width,height])# 设置位置
2、代码
# -*- coding: utf-8 -*-"""Created on Sun Sep 24 15:32:13 2017@author: ryoyun"""# 图中图import matplotlib.pyplot as pltimport numpy as np fig = plt.figure()x = [1,2,3,4,5,6,7]y = [1,3,4,2,5,8,6]left,bottom,width,height = 0.1,0.1,1.5,1.5ax1 = fig.add_axes([left,bottom,width,height])# 设置位置ax1.plot(x,y,'r')ax1.set_xlabel('x')ax1.set_ylabel('y')ax1.set_title('title')left,bottom,width,height = 0.2,0.6,0.25,0.25ax2 = fig.add_axes([left,bottom,width,height])ax2.plot(y,x,'b')ax2.set_xlabel('x')ax2.set_ylabel('y')ax2.set_title('title inside 1')plt.axes([.6,0.2,0.25,0.25])plt.plot(y[::-1],x,'g')plt.xlabel('x')plt.ylabel('y')plt.title('title inside 2')plt.show()
3、效果
四、次坐标轴(两个Y轴)
1、画几个散点一点都不难
ax2 = ax1.twinx() # 做镜像处理,ax2是ax1的镜像
2、代码
# -*- coding: utf-8 -*-"""Created on Sun Sep 24 15:44:29 2017@author: ryoyun"""# 次坐标轴import matplotlib.pyplot as pltimport numpy as npx = np.arange(0,10,0.1)y1= 0.05*x**2y2= -1*y1fig,ax1 = plt.subplots()ax2 = ax1.twinx() # 做镜像处理ax1.plot(x,y1,'g-')ax2.plot(x,y2,'b--')ax1.set_xlabel('X data') #设置x轴标题ax1.set_ylabel('Y1',color = 'g') #设置Y1轴标题ax2.set_ylabel('Y2',color = 'b') #设置Y2轴标题plt.show()
3、效果
五、Animation动画
1、代码
# -*- coding: utf-8 -*-"""Created on Sun Sep 24 15:51:25 2017@author: ryoyun"""# animation 动画from matplotlib import animationimport matplotlib.pyplot as pltimport numpy as npfig, ax = plt.subplots()x = np.arange(0,2*np.pi,0.01)line, = ax.plot(x,np.sin(x))def animate(i): line.set_ydata(np.sin(x+i/100)) return line,def init(): line.set_ydata(np.sin(x)) return line,ani = animation.FuncAnimation(fig =fig,func=animate,frames= 100,init_func=init, interval=20,blit=False,)plt.show()
2、效果
参考:莫烦python https://morvanzhou.github.io/tutorials/
阅读全文
1 0
- Matplotlib 入门(三):多图合并
- python matplotlib 入门系列三:坐标轴
- Matplotlib 入门(二):画图
- Matplotlib学习笔记(三)
- matplotLib入门
- Matplotlib入门
- matplotlib入门
- matplotlib入门-绘制简单折线图
- 机器学习笔记(一):matplotlib入门
- matplotlib库的简单使用(入门)
- Matplotlib 入门(一):基础操作
- 数据可视化(一)-Matplotlib简易入门
- python学习(三):matplotlib学习
- 数据可视化matplotlib的应用(三)
- python学习(三):matplotlib学习
- 莫烦-matplotlib学习笔记(三)
- Numpy快速入门教程(三):SciPy,Matplotlib
- 石子合并(三) 环形合并
- SpringBoot学习-(六)SpringBoot与Mybatis整合
- 【C#机房重构】用户sa失败-管道的另一端上无任何进程-无法找到表0
- win32飞机大战
- SQLAlchemy的基本使用
- 单例模式
- Matplotlib 入门(三):多图合并
- C#如何根据字符串动态的创建对象(类)
- 二分搜索树(一)
- 最大比合并算法 (Maximal Ratio Combining, MRC)
- 解决线性回归算法的过拟合问题-----LARS算法
- 网站资源
- 浅谈如何在Mac上安装GDB调试
- POJ-1664 放苹果
- linux用户组以及权限总结