溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python 一個figure上顯示多個圖像的實(shí)例

發(fā)布時間:2020-08-27 01:50:09 來源:腳本之家 閱讀:303 作者:gaoxiaobai666666 欄目:開發(fā)技術(shù)

方法一:主要是inshow()函數(shù)的使用

首先基本的畫圖流程為:

import matplotlib.pyplot as plt 
 
#創(chuàng)建新的figure
fig = plt.figure()
 
#必須通過add_subplot()創(chuàng)建一個或多個繪圖
#ax = fig.add_subplot(221)
 
#繪制2x2兩行兩列共四個圖,編號從1開始
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
 
#圖片的顯示
plt.show()

然后就會有四個在同一張圖上的figure

python 一個figure上顯示多個圖像的實(shí)例

然后我們可以用python中的Matplotlib庫中的,imshow()函數(shù)實(shí)現(xiàn)繪圖。imshow()可以用來繪制熱力圖

#coding=utf-8
import matplotlib.pyplot as plt 
import numpy as np
 
points = np.arange(-5,5,0.01)
 
xs,ys = np.meshgrid(points,points)
 
z = np.sqrt(xs**2 + ys**2)
 
#創(chuàng)建新的figure
fig = plt.figure()
 
#繪制2x2兩行兩列共四個圖,編號從1開始
ax = fig.add_subplot(221)
ax.imshow(z)
 
ax = fig.add_subplot(222)
#使用自定義的colormap(灰度圖)
ax.imshow(z,cmap=plt.cm.gray)
 
ax = fig.add_subplot(223)
#使用自定義的colormap
ax.imshow(z,cmap=plt.cm.cool)
 
ax = fig.add_subplot(224)
#使用自定義的colormap
ax.imshow(z,cmap=plt.cm.hot)
 
#圖片的顯示
plt.show()

python 一個figure上顯示多個圖像的實(shí)例

方法二:subplot的使用,在python中,可以用subplot繪制子圖。

常用方法:pl.subplot(121)第一個1代表1行,第二個2代表兩列,第三個1代表第一個圖。

  # -*- coding: utf-8 -*- 
 """ 
 演示二維插值。 
 """ 
 import numpy as np 
 from scipy import interpolate 
 import pylab as pl 
 import matplotlib as mpl 
  
 def func(x, y): 
  return (x+y)*np.exp(-5.0*(x**2 + y**2)) 
  
 # X-Y軸分為15*15的網(wǎng)格 
 y,x= np.mgrid[-1:1:15j, -1:1:15j] 
  
 fvals = func(x,y) # 計算每個網(wǎng)格點(diǎn)上的函數(shù)值 15*15的值 
 print len(fvals[0]) 
  
 #三次樣條二維插值 
 newfunc = interpolate.interp2d(x, y, fvals, kind='cubic') 
  
 # 計算100*100的網(wǎng)格上的插值 
 xnew = np.linspace(-1,1,100)#x 
 ynew = np.linspace(-1,1,100)#y 
 fnew = newfunc(xnew, ynew)#僅僅是y值 100*100的值 
  
 # 繪圖 
 # 為了更明顯地比較插值前后的區(qū)別,使用關(guān)鍵字參數(shù)interpolation='nearest' 
 # 關(guān)閉imshow()內(nèi)置的插值運(yùn)算。 
 pl.subplot(121) 
 im1=pl.imshow(fvals, extent=[-1,1,-1,1], cmap=mpl.cm.hot, interpolation='nearest', origin="lower")#pl.cm.jet 
 #extent=[-1,1,-1,1]為x,y范圍 favals為 
 pl.colorbar(im1) 
  
 pl.subplot(122) 
 im2=pl.imshow(fnew, extent=[-1,1,-1,1], cmap=mpl.cm.hot, interpolation='nearest', origin="lower") 
 pl.colorbar(im2) 
  
 pl.show() 

以上的代碼為二維插值中畫圖的演示。繪圖如下:

python 一個figure上顯示多個圖像的實(shí)例

以上這篇python 一個figure上顯示多個圖像的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI