【4.2】matplotlib-多图合并
一、多合一显示
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 plt
plt.figure()
plt.subplot(2,1,1) # 分成两行一列,起始点为1
plt.plot([0,1],[0,1]) # 设置xy轴范围
plt.subplot(2,3,4) # 分成两行三列,起始点位4
plt.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 plt
import 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 plt
import 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.5
ax1 = 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.25
ax2 = 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()
2.效果:
四、次坐标轴(两个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 plt
import numpy as np
x = np.arange(0,10,0.1)
y1= 0.05*x**2
y2= -1*y1
fig,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 animation
import matplotlib.pyplot as plt
import numpy as np
fig, 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()
3.效果:
其他
#组合图
import numpy as np
from matplotlib import pyplot as plt
# the random data
x = np.random.randn(1000)
y = np.random.randn(1000)
#自定义子图区域,需要先构建一个list,由左下角坐标,宽,高组成,来生成子图[x,y,w,h]
left_x,left_y=0.1,0.1
width,height=0.65,0.65
left_xh=left_x+width+0.02
left_yh=left_y+height+0.02
scatter_area=[left_x,left_y,width,height]
hist_x=[left_x,left_yh,width,0.2]
hist_y=[left_xh,left_y,0.2,height]
plt.figure(1, figsize=(8, 8))
#生成子图的方法用到plt.axes
area_scatter=plt.axes(scatter_area)
area_histx=plt.axes(hist_x)
area_histy=plt.axes(hist_y)
#画散点图
area_scatter.scatter(x, y)
#统计散点图,画概率分布图
#设置概率分布图的bins的宽度
binwidth=0.25
#统计最大的x值,最大的y值
#np.fabs()返回绝对值
xymax=np.max([np.max(np.fabs(x)),np.max(np.fabs(y))])
#bin的数量
N_bins=int(xymax/binwidth)+1
#最大坐标
lim=N_bins*binwidth
#最小坐标
nlim=-lim
#坐标轴的分布
bins=np.arange(nlim,lim+binwidth,binwidth)
#根据取得的坐标分布,将散点图的坐标轴与此对应
area_scatter.set_xlim(nlim,lim)
area_scatter.set_ylim(nlim,lim)
#画出概率分布图
area_histx.hist(x, bins=bins)
area_histy.hist(y, bins=bins, orientation='horizontal')
#设置概率分布图的坐标
area_histx.set_xlim(area_scatter.get_xlim())
area_histy.set_ylim(area_scatter.get_ylim())
plt.show()
参考资料
这里是一个广告位,,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn