Home > Back-end >  How to display multiple images which numbers of image not same for every attempt
How to display multiple images which numbers of image not same for every attempt

Time:11-27

I want to display some images with matplotlib with fig.add_subplot but for some attempts I faced some errors like below:

Traceback (most recent call last):
  File "/home/---/Documents/---/---/dataset.py", line 134, in <module>
    display_dicom(dicom,target["mask"])
  File "/home/---/Documents/---/---/dataset.py", line 123, in display_dicom
    fig.add_subplot(rows,cols,i 2)
  File "/home/---/.pyenv/versions/3.8.13/envs/---/lib/python3.8/site-packages/matplotlib/figure.py", line 745, in add_subplot
    ax = subplot_class_factory(projection_class)(self, *args, **pkw)
  File "/home/---/.pyenv/versions/3.8.13/envs/---/lib/python3.8/site-packages/matplotlib/axes/_subplots.py", line 36, in __init__
    self.set_subplotspec(SubplotSpec._from_subplot_args(fig, args))
  File "/home/---/.pyenv/versions/3.8.13/envs/---/lib/python3.8/site-packages/matplotlib/gridspec.py", line 612, in _from_subplot_args
    raise ValueError(
ValueError: num must be 1 <= num <= 2, not 3
Traceback (most recent call last):
  File "/home/---/Documents/---/---/dataset.py", line 133, in <module>
    display_dicom(dicom,target["mask"])
  File "/home/---/Documents/---/---/dataset.py", line 123, in display_dicom
    plt.imshow(mask[i],cmap=plt.cm.bone)
IndexError: index 69 is out of bounds for dimension 0 with size 69

I want to compute row and col in plt.figure automatically. What is the formula that doesn't crash the code?

I tried the following func to display it. dicom and mask are torch.tensors. When I select the row and col by hand, it works well.

def display_dicom(dicom,mask):

    count,width,height = mask.shape
    if count == 0:
        count = 1

    fig = plt.figure(figsize=(10,10))
    rows= int(math.sqrt(count) 1)
    cols = int(math.sqrt(count) 1)

    
    fig.add_subplot(rows,cols, 1)
    plt.imshow(dicom, cmap=plt.cm.bone)  # set the color map to bone
    plt.title("dicom")

    for i in range(2,count 2):
        fig.add_subplot(rows,cols,i)
        plt.imshow(mask[i],cmap=plt.cm.bone)
        plt.title(f"mask {i 1}")

    plt.show()

Sample plots are how I want to get the plot: 83 image 10 image 2 image

CodePudding user response:

I rearrange the display_dicom function as below. It is works fine now. It's not the best solution.

def display_dicom(dicom,mask):

    count,width,height = mask.shape
    if count == 0:
        count = 1 # np.zeros empty mask
    
    number_of_images = count   1 # masks   dicom

    fig = plt.figure(figsize=(10,10))
    rows= math.ceil(math.sqrt(number_of_images))
    cols = rows
    
    fig.add_subplot(rows,cols, 1)
    plt.imshow(dicom, cmap=plt.cm.bone)  # set the color map to bone
    plt.title("dicom")

    for i in range(count):
        fig.add_subplot(rows,cols,i 2)
        plt.imshow(mask[i],cmap=plt.cm.bone)
        plt.title(f"mask {i 1}")

    plt.show()

CodePudding user response:

How about using fig, axes = plt.subplots(nrows = rows, ncols = cols, figsize = (10, 10)) and then you iterate over the axes and do .imshow on each one of them? axes will be a 2D array of size nrows x ncols.

  • Related