【问题标题】:Python matplotlib - Image titles not displaying in loopPython matplotlib - 图像标题未在循环中显示
【发布时间】:2018-02-18 22:28:58
【问题描述】:

我希望以图像名称作为标题循环显示图像。每个图像都显示在循环中,但标题不显示。

在下面的函数中,img_list 包含具有以下[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()

谢谢。

【问题讨论】:

    标签: python matplotlib imshow


    【解决方案1】:

    如果您重新分配plt.titletitle() 函数将被字符串覆盖。

    您需要调用 plt.title() 函数。

    plt.title(image_file_name)
    

    【讨论】:

    • 谢谢 - 成功了。对于找到此答案的其他人,在进行更改后需要重新启动内核。否则你会得到一个''str' object is not callable'”错误。
    猜你喜欢
    • 2011-12-07
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2019-11-25
    • 2016-06-07
    • 2021-05-26
    • 1970-01-01
    相关资源
    最近更新 更多