【问题标题】:Add a color bar in imported figure (Python)在导入的图形中添加颜色条(Python)
【发布时间】:2020-06-03 07:28:58
【问题描述】:

我有一个灰度图。我想知道如何使用 Python 在这种导入的图形中添加颜色条。我知道如何绘制二维数组并添加彩条,但我不知道如何为导入的图形执行类似的任务。我也想保留原始图像的大小。我用

导入图
from PIL import Image
im= Image.open("gray.png")

提前致谢。

`

【问题讨论】:

  • 你想要这样的imgur.com/a/QSYOfgk
  • @Snehil 是的!正是这个。你能给我更多的信息吗?这个图上面的命令你用了吗?怎么样?

标签: python python-imaging-library colorbar


【解决方案1】:
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt

def display_image_in_actual_size(im_path):

    dpi = 80
    im_data = plt.imread(im_path)
    height, width, depth = im_data.shape
    # What size does the figure need to be in inches to fit the image?
    figsize = width / float(dpi), height / float(dpi)

    # Create a figure of the right size with one axes that takes up the full figure
    fig = plt.figure(figsize=figsize)
    ax = fig.add_axes([0, 0, 1, 1])


    #ax.axis('off') #  uncommenting this will result  a plot without axis !
    # configures size of colorbar 
    divider = make_axes_locatable(ax)
    im=plt.imshow(im_data)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    plt.colorbar(im, cax=cax)

    ax.imshow(im_data, cmap='gray')
    ax.set(xlim=[-10, width - 0.5], ylim=[height - 0.5, -0.5], aspect=1)

    plt.savefig('last_image.png')    #saving new image
    plt.show()

display_image_in_actual_size("gray.png")

我已经改编了here的部分答案

【讨论】:

  • 非常感谢。这正是我一直在寻找的。赞成并接受为正确答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 2020-10-07
  • 1970-01-01
  • 2015-06-29
  • 2015-10-07
  • 2015-11-07
相关资源
最近更新 更多