matplotlib subplot画子图
文章目录
- 1 举例
- 2 设置
- 2.1 标题
- 2.2 对单个子图的设置
- 2.3 使用for简化程序
- 3 scatter画图
- 3.1 scatter在log坐标下画图
- 3.2 scatter在semilogx画图
- 4 healpy画子图
1 举例
以下图为例:在一个figure上有三个图:
plt.subplot(221) #表示有2行2列第一个图
plt.plot(x1,y1)
plt.subplot(222) #表示有2行2列第二个图
plt.loglog(x2,y2)
plt.subplot(212)
plt.loglog(x3,y3)plt.show()
上面的前两个都好理解,plt.subplot(212)比较不好理解。
因为第三个图实际上是占用了(223)和(224)两个图的位置;现在规定当出现这种一个图占多个图位置时要重新划分,相当于是(211)和(212),即2行1列,而(211)已经被第一第二个图占了(此时要把它们一起看成一列),现在是(212)表示第2行第1列第2个图。
当然,更方便的是使用(参考:https://blog.csdn.net/kepengs/article/details/84886457)
fig,ax = plt.subplots()
等价于
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
这样就不用每次画图都先写plt.figure()了
完整代码如下,计算的是黑体谱
import numpy as np
import matplotlib.pyplot as plt
import pysmdef Bv(nu, T):kB = 1.3806505*10**(-23) # J/Kh = 6.6260693*10**(-34+9) # J·s, dimension becomes J/GHz by plus 9c = 299792458return 2*h*nu**3/c**2*1/(np.exp(h*nu/(kB*T))-1)# plot CMB power spectrum
Tcmb = 2.725
nu = np.arange(10,1000)
CMB_power_spect = Bv(nu,Tcmb)
# get max value of Bv and corresponding value of nu;
max_Bv = max(CMB_power_spect)
max_ind = list(CMB_power_spect).index(max_Bv)
max_Bv_to_nu = nu[max_ind]
print("maximun value:>>>(%e,%d)"%(max_Bv,max_Bv_to_nu))
# subplot
plt.figure() #这句可有可无
plt.subplot(221)
plt.plot(nu,CMB_power_spect)
plt.subplot(222)
plt.loglog(nu,CMB_power_spect)
plt.subplot(212)
plt.loglog(nu,CMB_power_spect*10**26*pysm.common.convert_units('Jysr','uK_RJ',nu))plt.show()
2 设置
2.1 标题
更多设置可以参考官网的例子
注意: 一旦子图超过两个,那么就不能只用ax[0]这样表示,而是要用ax[0,0]表示第一个子图。
fig,ax = plt.subplots(2,1,figsize=(14,8)) #设置figsize
ax[1].semilogx(m_tt[2:],np.sqrt(2*m_tt*Gamma_m)[2:],label='$\Gamma_m$')
ax[1].set_xlabel('$m$') #xlabel
ax[1].set_ylabel('$(2m\Gamma_m)^{1/2}$') #ylabel
ax[0].semilogx(ell_tt,np.sqrt(Dl_tt/2*bl**2),label='Cl')
ax[0].set_xlabel('$\ell$')
ax[0].set_ylabel('$(\ell(2\ell+1)/4\pi)^{1/2}$')
设置每个子图标题:
fig,ax = plt.subplots(2,3,figsize=(14,8))
ax[0,0].plot(ell, ell*(ell+1)/2/np.pi*Cl_cmb[0], ell, cleaned_Dl[0])
ax[0,0].set_title('TT')
ax[0,1].plot(ell, ell*(ell+1)/2/np.pi*Cl_cmb[1], ell, cleaned_Dl[1])
ax[0,1].set_title('EE')
ax[0,2].plot(ell, ell*(ell+1)/2/np.pi*Cl_cmb[2], ell, cleaned_Dl[2])
ax[0,2].set_title('BB')
ax[1,0].plot(ell, ell*(ell+1)/2/np.pi*Cl_cmb[3], ell, cleaned_Dl[3])
ax[1, 0].set_title('TE')
ax[1,1].plot(ell, ell*(ell+1)/2/np.pi*Cl_cmb[4], ell, cleaned_Dl[4])
ax[1, 1].set_title('EB')
ax[1,2].plot(ell, ell*(ell+1)/2/np.pi*Cl_cmb[5], ell, cleaned_Dl[5])
ax[1,2].set_title('TB')
plt.legend(['original CMB', 'ILC CMB'])
设置总标题
fig, ax = plt.subplots(2, 1,figsize=(10,7))
fig.suptitle('semilogx of $m\Gamma_m$ and $D_\ell$') #设置总标题
ax[1].semilogx(m_tt, m_tt*Gamma_m)
ax[1].legend(['theory', 'ring_data'])
ax[1].set_xlabel('$m$')
ax[1].set_ylabel('$m\Gamma_m$')
ax[0].semilogx(ell_tt_bin, Dl_bin)
ax[0].set_xlabel('$\ell$')
ax[0].set_ylabel('$\ell(\ell+1)C_\ell/2\pi$')
设置每个子图注释:
for i in range(2, np.shape(dTrj_dt)[1]):ax[0].plot(f, dTrj_dt[:, 2+np.shape(dTrj_dt)[1]-i-1], label=str(i-2)+'th')ax[0].set_title('dTrj/dT (K_RJ)')ax[0].set_xlabel('freq (GHz)')ax[0].set_ylabel('dTrj/dT')ax[1].plot(f, RJ2CMB*dTrj_dt[:, 2+np.shape(dTrj_dt)[1]-i-1], label=str(i-2)+'th')ax[1].set_title('dTrj/dT (K_cmb)')ax[1].set_xlabel('freq (GHz)')ax[1].set_ylabel('dTrj/dT')
ax[0].legend()
ax[1].legend()
#最后两行用于展示注释,
最后两行是展示子图注释的,
2.2 对单个子图的设置
fig, ax = plt.subplots(2, 1, figsize=(14, 8))
ax[1].semilogx(m_tt, np.sqrt(2*m_tt*Gamma_m), m_tt, np.sqrt(2*m_tt*Gamma_m1))
ax[1].legend(['theory','ring_data']) #标签
ax[1].set_xlabel('$m$')
ax[1].set_ylabel('$(2m\Gamma_m)^{1/2}$')
ax[0].semilogx(ell_tt, np.sqrt(Dl_tt/2*bl**2), label='Cl')
ax[0].set_xlabel('$\ell$')
ax[0].set_ylabel('$(\ell(2\ell+1)C_\ell/4\pi)^{1/2}$')
2.3 使用for简化程序
上面都是单独每个子图设置的,其实可以使用for来简化程序
fig, ax = plt.subplots(2, 3, figsize=(14, 10))
count = 0
for i in range(2):for j in range(3):if not (count>=n_bands):#防止指标超出ax[i, j].loglog(m, gamma_cmb[:m_len], m,gamma_c[:m_len], m, gamma[count])ax[i, j].legend(['gamma_cmb', 'gamma_clean', 'gamma_tot'])ax[i, j].set_title(str(bands[count])+' GHz')count += 1plt.show()
此外,可以将需要标注的内容先用列表收集起来,然后统一调用
如
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
plt.figure(figsize=(10,10))
for i in range(25):plt.subplot(5,5,i+1) #5行5列,会按顺序从左到右从上到下进行画图plt.xticks([]) #不需要刻度plt.yticks([]) #不需要刻度plt.imshow(train_images[i], cmap=plt.cm.binary)plt.xlabel(class_names[train_labels[i]])
plt.show()
3 scatter画图
3.1 scatter在log坐标下画图
fig = plt.figure()
ax = plt.gca()
ax.scatter(ell_tt_bin, Dl_bin, s=3, c='r', label='Dl_bin (log)')
ax.loglog(ell_tt_bin, Dl_bin)
ax.set_title('bin_size = round('+str(rt)+'*'+str(q)+'^i) m_start='+str(start_m))
ax.set_xlabel('$\ell$')
ax.set_ylabel('$D_\ell$')
# ax.set_ylim(0, 6*1e-8)
# plt.xlim(-10, 600)
plt.legend()
以上scatter之所以是在log坐标下是因为我们又画了一个plt.loglog的线,如果我们只想要散点图,那么直接使用loglog并设置曲线图像为圆圈`o``也可以达到目的,如下:
plt.loglog(x,y,'o',markersize=1)
3.2 scatter在semilogx画图
fig, ax = plt.subplots(2, 1,figsize=(10,7))
fig.suptitle('semilogx of $m\Gamma_m$ and $D_\ell$')
ax[1].semilogx(m_tt, m_tt*Gamma_m)
ax[1].scatter(m_tt, m_tt*Gamma_m,s=3,c='r')
ax[1].legend(['theory', 'ring_data'])
ax[1].set_xlabel('$m$')
ax[1].set_ylabel('$m\Gamma_m$')
ax[0].semilogx(ell_tt_bin, Dl_bin)
ax[0].scatter(ell_tt_bin, Dl_bin,s=3,c='r')
ax[0].set_xlabel('$\ell$')
ax[0].set_ylabel('$\ell(\ell+1)C_\ell/2\pi$')
可以看到,是属于log还是semilogx不是看scatter如何设置,而是画图时scatter随着所画的图所属的坐标系撒点,比如上面ax[1].semilogx(m_tt, m_tt*Gamma_m)
表示在semilogx下画图,那么下面的scatter撒点就会跟随这个semilogx,如果上面用的是loglog,那么scatter也就会属于loglog
4 healpy画子图
注意在healpy是从1开始数的,而python是0开始,所以第一个是i+1
matplotlib subplot画子图相关推荐
- 用matplotlib.subplot()画子图并共享y坐标轴
有时候想要把几张图放在一起plot,比较好对比,subplot和subplots都可以实现,具体对比可以查看参考博文.这里用matplotlib库的subplot来举个栗子. 数据长什么样 有两个数据 ...
- python画子图_Python使用add_subplot与subplot画子图操作示例
本文实例讲述了Python使用add_subplot与subplot画子图操作.分享给大家供大家参考,具体如下: 子图:就是在一张figure里面生成多张子图. Matplotlib对象简介 Figu ...
- python add_subplot_Python使用add_subplot与subplot画子图操作
这篇文章主要介绍了Python使用add_subplot与subplot画子图操作,涉及Python使用matplotlib模块进行图形绘制的相关操作技巧,需要的朋友可以参考下 本文实例讲述了Pyth ...
- python add subplot_Python使用add_subplot与subplot画子图操作示例
本文实例讲述了Python使用add_subplot与subplot画子图操作.分享给大家供大家参考,具体如下: 子图:就是在一张figure里面生成多张子图. Matplotlib对象简介 Figu ...
- python如何画出多个独立的图片_python使用matplotlib:subplot绘制多个子图的示例
数据可视化的时候,常常需要将多个子图放在同一个画板上进行比较,python 的matplotlib包下的subplot可以帮助完成子功能. part1 绘制如下子图 1 2 3 4 5 6 7 8 9 ...
- 【matplotlib笔记】plt.subplot()绘制子图
绘制子图 面向过程的方式绘图 如何绘制如下的图呢? 上半部分:看做2行2列,第1行第1列.第1行第2列分别放一张图 下半部分:看做2行1列,第2行的1列放一张图 通过plt.subplot()绘制子图 ...
- 【科研分享】Matplotlib 绘制多子图(subplot)进行实验结果分析
Matplotlib 绘制多子图(subplot)进行实验结果分析 本文主要总结于本人近期的科研经历,在最后撰写论文试验分析的时候曾经被导师要求绘制一幅2*8的16子图的超参数分析结果图.所以踩了 ...
- python怎么画两张图_python中如何用subplot画多个子图?
开始讲今天的内容之前,先说一点题外话.上学的时候地理课上会画一些等高图.小地图之类的,那时候小编就对地理产生了浓厚的兴趣,不过最后因为种种原因还是没有选择继续学习地理方面的知识了.接触了python之 ...
- python——plt.figure()画子图(双轴图)双Y轴实例
话对比图,如果两个数量级的纵坐标,我们要进行趋势比较,放在同一坐标轴,某一个往往被压缩的的很小.所以需要左右双Y轴画图. 1.随便获取数列,对应的索引化成一样就可以了,下面是量化的数据处理,可以不用看 ...
最新文章
- 0x01.基本算法 — 位运算
- 手机+笔记本上NET网教程--5步让你轻松上网
- 网站关键词优化从这几方面下手效果会更好!
- linux与安卓系统目录,android系统架构及源码目录结构
- UVA 725 Division(除法)
- linux prel安装_Linux下Perl的安装(转)
- P3225-[HNOI2012]矿场搭建【tarjan,图论】
- python封装介绍_谈python3的封装
- 主板后置音频接口图解_颜值出众、用料靠谱——华擎(ASRock)Z490 Extreme4极限玩家主板 简析...
- 德国数字化进展迅速,远程发送诉讼仅需6小时
- UI设计素材|社交界面模板
- 2017杭州云栖大会FAQ(持续更新中)
- hutool BigExcelWriter 下的autoSizeColumnAll异常问题
- 小程序wafer2操作数据库
- 【车道线检测与寻迹】2月17日 车道线检测carry边缘检测+Hough哈夫变换法
- 拓端tecdat|GIS遥感数据可视化评估:印度河流域上部的积雪面积变化
- Haswell 事务内存相关的一些资源介绍
- html+css的响应式个人简历
- 找不到项目 该项不在计算机中,Win7提示“找不到该项目”怎么办 Win7提示“找不到该项目”解决方法...
- android文献翻译,Android外文文献翻译