0

I need to plot different images with pyplot. As you can see there are a lot of white are and small images.

I want to make images larger and closer. How can I do that?

Example result of the current situation: Current situation of the code

The code I wrote:

def draw_images(self, names = None, show = False):
    if names == None:
        names = self.images_list
    number_of_items = len(names)
    box_size_h = int(np.sqrt(number_of_items - 1)) + 1
    box_size_w = int((number_of_items // box_size_h))
    if box_size_w < (number_of_items / box_size_h):
        box_size_w = box_size_w + 1
    for i, name in enumerate(names):
        self.fig.add_subplot(box_size_w, box_size_h, i+1)
        plt.imshow(cv2.cvtColor(self.images_list[name], cv2.COLOR_BGR2RGB))
        plt.axis("off")
        plt.title(name)
    now = datetime.now()
    dt_string = now.strftime("%d_%m_%Y_%H_%M_%S")
    plt.savefig("figures/" + dt_string + ".jpg")
    figure = upload_image("figures/" + dt_string + ".jpg")
    if show:
        cv2.imshow("FIGURE", figure)
        cv2.waitKey(0)
    return figure

I am adding images as subplots basically. But I do not how to make them bigger and closer.

Thanks in advance.

  • When you create the figure, specify constrained_layout=True. That will reduce the horizontal space. The vertical space is a consequence of you having a figure that is tall, and the subplots having a fixed aspect ratio. Make your figure shorter. – Jody Klymak Oct 02 '21 at 09:30

0 Answers0