简体   繁体   English

将多个 matplotlib 图形合并为一个

[英]Combine multiple matplotlib figures into one

I have a function that takes an enhanced dicom file and does the following:我有一个函数,它采用增强的 dicom 文件并执行以下操作:

  1. Use a for loop that creates a single slice from the dicom file and then index it into a smaller array around a set of six specks.使用 for 循环从 dicom 文件创建单个切片,然后将其索引到围绕一组六个斑点的较小数组中。
  2. A second for loop that draws circles around the six specks and one for the background第二个 for 循环在六个斑点周围绘制圆圈,一个用于背景
  3. Show the image with all the circles plotted and the current slice location.显示所有绘制的圆圈和当前切片位置的图像。

When I call the function I can create multiple images that are displayed in separate figures.当我调用该函数时,我可以创建多个显示在不同图形中的图像。 So, how can I combine all these figures into one?那么,我怎样才能将所有这些数字合二为一呢? Ideally I want to display them in a 3x3 grid.理想情况下,我想以 3x3 网格显示它们。

Here is my current code:这是我当前的代码:

import matplotlib.pyplot as plt
import pydicom # Used for opening DICOM files
import numpy as np # General mathematical package
from pylab import text

# Import DICOM files
filename = "U:/File location" 
ds = pydicom.dcmread(filename)

# Speck Locations
centerSpeck = (1690, 1477)
centerSpeck2 = (100, 100)
twelveOclockSpeck = (45, 84)
twoOkclockSpeck = (66, 148)
tenOclockSpeck = (136, 147)
fiveOclockSpeck = (157, 82)
sevenOclockSpeck = (102, 41)
backgroundSignalValue = (71, 63)

speckLocations = np.array([centerSpeck2, twelveOclockSpeck, twoOkclockSpeck, fiveOclockSpeck, sevenOclockSpeck, tenOclockSpeck, backgroundSignalValue])

# Function that draws a circle around a given pixel
def drawCircle(arrayToPLot, zeroIndex, oneIndex, specks, r = 10):
    x = speckLocations[:,1] # get x axis variables from the speckLocations array
    y = speckLocations[:,0] # get y axis variables from the speckLocations array
    for i in range(zeroIndex,oneIndex,1): # For loop, note that the function range is: range(start, stop, step)
        tempIm = arrayToPLot.pixel_array[i,:,:].astype(float) # Get one slice as float
        slicedArray = tempIm[centerSpeck[0]-100:centerSpeck[0]+100, centerSpeck[1]-100:centerSpeck[1]+100].astype(float)
        for x,y in (speckLocations):
            plt.imshow(slicedArray, cmap='gray')
            circle = plt.Circle((y, x),r, fc='none', ec="red")
            plt.gca().add_patch(circle)    
            text(10, 180, i, fontsize=12, color='red') # Print the current slice on the image

        plt.show() # Plot each slice with circles drawn around all the specks and the background signal value location

#Call the function  
drawCircle(ds, 33, 34, speckLocations)

In the following we'll use the plt.subplots method to produce a figure and a grid of axes , the Matplotlib objects that someone understands as subplots ...在下文中,我们将使用plt.subplots方法生成图形和axes网格,即有人理解为子图的 Matplotlib 对象......

Iteration on a 2D grid first give you a row of the grid, a second iteration on the row gives you the individual subplot.二维网格上的迭代首先为您提供一行网格,该行上的第二次迭代为您提供单个子图。 When we have singled out an axes, it's time to call your function (you have the responsibility to differentiate the data to plot because it's not clear from your question what you want to do) but with an additional argument, the current axes.当我们挑选出一个轴时,是时候调用您的函数(您有责任区分要绘制的数据,因为从您的问题中不清楚您想要做什么),但是有一个额外的参数,即当前轴。

fig, ax_grid = plt.subplots(3,3)
for ax_row in ax_grid:
    for ax in ax_row:
        drawCircle(ax, ds, 33, 34, speckLocations)
plt.tight_layout()
plt.show()

At the end we show the figure, calling tight_layout to get a better arrangement of the subplots.最后我们展示图,调用tight_layout以获得更好的子图排列。


Of course we have to modify the circle plotting function... first we add ax to the list of arguments, next we modify the calls to plt methods to use the methods of the ax object:当然,我们必须修改圆绘图函数……首先我们将ax添加到参数列表中,接下来我们修改对plt方法的调用以使用ax对象的方法:

def drawCircle(ax, arrayToPLot, zeroIndex, oneIndex, specks, r = 10):
    ...
    ax.imshow(...)
    ...
    ax.add_patch(circle)
    ax.text(10, 180, i, fontsize=12, color='red')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM