Python matplotlib - Image titles not displaying in loop
12,960
If you reassign plt.title
, the title()
function is overwritten by a string.
Instead you need to call the plt.title()
function.
plt.title(image_file_name)
![dvd940](https://i.stack.imgur.com/hoSYn.png?s=256&g=1)
Author by
dvd940
My day job doesn't have much to do with code but I like to dabble in Python and R in my spare time.
Updated on June 05, 2022Comments
-
dvd940 about 2 years
I wish to display images in a loop with the image name as a title. Each image displays in the loop but the title does not.
In the function below, the
img_list
contains lists with the following[image, image_title]
.def display_images(img_list, cmap='gray', cols = 2, fig_size = (10, 10) ): """ Display images in img_list """ i = 1 # for subplot num_images = len(img_list) num_rows = num_images / cols plt.figure(figsize=fig_size) for image in img_list: image_file_name = image[1] plt.subplot(num_rows, cols, i) plt.title = image_file_name plt.imshow(image[0], cmap=cmap) i += 1 plt.show()
Thanks.
-
dvd940 almost 7 yearsThank you - that worked. And for others who find this answer, the kernel needs to be restarted after making the change. Otherwise you will get a ''str' object is not callable'" error.