Several digits in one window

I want to create a function that displays a set of numbers on the screen in one window. At this point, I am writing this code:

import pylab as pl def plot_figures(figures): """Plot a dictionary of figures. Parameters ---------- figures : <title, figure> dictionary """ for title in figures: pl.figure() pl.imshow(figures[title]) pl.gray() pl.title(title) pl.axis('off') 

It works fine, but I would like to be able to draw all the shapes in one window. And this code is not. I read something about the subtitle, but it looks pretty complicated.

+5
source share
6 answers

You can define a function based on the subplots command (note s at the end, different from the subplot command indicates urinieto) matplotlib.pyplot .

Below is an example of such a function, based on yours, which allows you to build several axes in a figure. You can determine the desired number of rows and columns in the layout of the picture.

 def plot_figures(figures, nrows = 1, ncols=1): """Plot a dictionary of figures. Parameters ---------- figures : <title, figure> dictionary ncols : number of columns of subplots wanted in the display nrows : number of rows of subplots wanted in the figure """ fig, axeslist = plt.subplots(ncols=ncols, nrows=nrows) for ind,title in enumerate(figures): axeslist.ravel()[ind].imshow(figures[title], cmap=plt.gray()) axeslist.ravel()[ind].set_title(title) axeslist.ravel()[ind].set_axis_off() plt.tight_layout() # optional 

In fact, the function creates the number of axes in the drawings in accordance with nrows number of rows ( nrows ) and columns ( ncols ), and then ncols over the list of axes to build images and adds a title for each of them.,

Note that if there is only one image in your dictionary, your previous syntax, plot_figures(figures) will work, since nrows and ncols are set to 1 by default.

An example of what you can get:

 import matplotlib.pyplot as plt import numpy as np # generation of a dictionary of (title, images) number_of_im = 6 figures = {'im'+str(i): np.random.randn(100, 100) for i in range(number_of_im)} # plot of the images in a figure, with 2 rows and 3 columns plot_figures(figures, 2, 3) 

ex

+11
source

You must use subplot .

In your case, it will be something like this (if you want them to be on top of each other):

 fig = pl.figure(1) k = 1 for title in figures: ax = fig.add_subplot(len(figures),1,k) ax.imshow(figures[title]) ax.gray() ax.title(title) ax.axis('off') k += 1 

See the documentation for other options.

+2
source

I'm not sure why I could not get the accepted answer using my data, but here is my solution if someone wants to use it.

X_train is a list of images in this case, and y_train is a numeric label. The index variable is randomly selected, and we have a stop number of 8 to kill the operation.

The numbers that display the graph are a dictionary of the displayed images / labels, and 2 represent the rows and 4 represent the columns.

 number_to_stop = 8 figures = {} for i in range(number_to_stop): index = random.randint(0, n_train-1) figures[y_train[index]] = X_train[index] plot_figures(figures, 2, 4) 

enter image description here

0
source

Based on the answer from: How to correctly display multiple images on the same figure? Here is another method:

 import math import numpy as np import matplotlib.pyplot as plt def plot_images(np_images, titles = [], columns = 5, figure_size = (24, 18)): count = np_images.shape[0] rows = math.ceil(count / columns) fig = plt.figure(figsize=figure_size) subplots = [] for index in range(count): subplots.append(fig.add_subplot(rows, columns, index + 1)) if len(titles): subplots[-1].set_title(str(titles[index])) plt.imshow(np_images[index]) plt.show() 
0
source

You can also do this:

 import matplotlib.pyplot as plt f, axarr = plt.subplots(1, len(imgs)) for i, img in enumerate(imgs): axarr[i].imshow(img) plt.suptitle("Your title!") plt.show() 
0
source

If you want to group several shapes in one window, you can do something. like this:

 import matplotlib.pyplot as plt import numpy as np img = plt.imread('C:/.../Download.jpg') # Path to image img = img[0:150,50:200,0] # Define image size to be square --> Or what ever shape you want fig = plt.figure() nrows = 10 # Define number of columns ncols = 10 # Define number of rows image_heigt = 150 # Height of the image image_width = 150 # Width of the image pixels = np.zeros((nrows*image_heigt,ncols*image_width)) # Create for a in range(nrows): for b in range(ncols): pixels[a*image_heigt:a*image_heigt+image_heigt,b*image_heigt:b*image_heigt+image_heigt] = img plt.imshow(pixels,cmap='jet') plt.axis('off') plt.show() 

As a result, you will receive: enter image description here

0
source

All Articles