Python 如何根据图像上的颜色绘制图例?

Python 如何根据图像上的颜色绘制图例?,python,matplotlib,legend,Python,Matplotlib,Legend,现在我得到了一个像图1一样的图像,不同的颜色意味着不同的事情,我想在图1的底部添加一个图例(图2),怎么做?我有每种颜色的rgb值 图1: 图2: 这是我得到的代码: # coding=utf-8 import matplotlib matplotlib.use('Agg') import h5py import numpy from PIL import Image from PIL import ImageDraw import matplotlib.pyplot as plt impo

现在我得到了一个像图1一样的图像,不同的颜色意味着不同的事情,我想在图1的底部添加一个图例(图2),怎么做?我有每种颜色的rgb值

图1:

图2:

这是我得到的代码:

# coding=utf-8
import matplotlib
matplotlib.use('Agg')
import h5py
import numpy
from PIL import Image
from PIL import ImageDraw
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
import sys
table={
    k:v for k,v,n in [

[
        127,
        [
            100,
            100,
            100
        ],
        "NO DATA"
    ],
    [
        126,
        [
            0,
            0,
            0
        ],
        "SPACE"
    ],
    [
        0,
        [
            200,
            255,
            255
        ],
        "CLEAR"
    ],
    [
        2,
        [
            0,
            0,
            244
        ],
        "WATER CLOUD"
    ],
    [
        3,
        [
            32,
            165,
            225
        ],
        "ICED CLOUD"
    ],
    [
        4,
        [
            33,
            255,
            170
        ],
        "MIXED CLOUD"
    ],
    [
        5,
        [
            255,
            0,
            0
        ],
        "CIRRUS CLOUD"
    ],
    [
        6,
        [
            180,
            20,
            255
        ],
        "Opaque cloud"
    ],
    [
        7,
        [
            105,
            255,
            0
        ],
        "OVERLAP CLOUD"
    ],
    [
        9,
        [
            224,
            180,
            0
        ],
        "UNKNOWN CLOUD"
    ]
]
}


def main(_,fn,out):
    with h5py.File(fn) as f:
        data = f['EVB1'].value
    w,h = data.shape
    ret = numpy.zeros((w,h,3),'u1')
    for i in (0,2,3,4,5,6,7,9,126,127):
        ret[data==i]=table[i]

    Image.fromarray(ret,mode="RGB").save(out)
    image = Image.open(out)
    my_dpi = 100.


    # Set up figure
    fig = plt.figure(figsize=(float(image.size[0]) / my_dpi,float(image.size[1]) / my_dpi), dpi=my_dpi)
    ax = fig.add_subplot(111)

# Set the gridding interval: here we use the major tick interval
    myInterval = 249.9
    loc = plticker.MultipleLocator(base=myInterval)
    # ax=plt.gca()
    ax.xaxis.set_major_locator(loc)
    ax.yaxis.set_major_locator(loc)

    ax.set_xticklabels(['60', '70', '80', '90', '100', '110', '120', '130', '140'])
# ax.set_xticklabels(np.arange(70,150,10))
    ax.set_yticklabels(('70', '60', '50', '40', '30', '20', '10', '0'))
#


    out1 = out.split('/')[-1].split('.')[0].split('V0001')[0]

    ax.set_title(out1,fontsize = 20)

# Add the grid
    ax.grid(which='major', axis='both', linestyle='-')

# Add the image
    ax.imshow(image)


# Save the figure
    fig.savefig(out)



if __name__ == '__main__':
    main(*sys.argv)

我无法正确显示汉字,但您应该了解基本情况:

# coding=utf-8
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np

x = np.linspace(0,1,100)
y = np.linspace(0,1,100)

X,Y = np.meshgrid(x,y)

array = np.sin(X)*np.cos(Y)

plt.imshow(array)

legend_data = [
   [
    127,
    [
        100,
        100,
        100
    ],
    "无数据区"
],
[
    126,
    [
        0,
        0,
        0
    ],
    "外太空"
],
[
    0,
    [
        200,
        255,
        255
    ],
    "晴空"
],
[
    2,
    [
        0,
        0,
        244
    ],
    "水云"
],
[
    3,
    [
        32,
        165,
        225
    ],
    "过冷水云"
],
[
    4,
    [
        33,
        255,
        170
    ],
    "混合云"
],
[
    5,
    [
        255,
        0,
        0
    ],
    "厚冰云"
],
[
    6,
    [
        180,
        20,
        255
    ],
    "卷云"
],
[
    7,
    [
        105,
        255,
        0
    ],
    "多层云"
],
[
    9,
    [
        224,
        180,
        0
    ],
    "不确定"
]
]    
handles = [
    Rectangle((0,0),1,1, color = (v/255 for v in c)) for k,c,n in legend_data
]
labels = [n for k,c,n in legend_data]

plt.legend(handles,labels)
plt.show()
结果如下所示:

绘图只是一个占位符,因为我没有您的输入数据。关键行是从表和最后的label命令中生成矩形
句柄
标签的行

编辑

如果希望图例严格位于绘图下方,可以通过为图例定义第二个轴来实现:

from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.gridspec import GridSpec
import numpy as np

from matplotlib.font_manager import FontProperties
ChineseFont = FontProperties('SimHei')

x = np.linspace(0,1,100)
y = np.linspace(0,1,100)

X,Y = np.meshgrid(x,y)

array = np.sin(X)*np.cos(Y)

gs = GridSpec(6,1)

fig = plt.figure(figsize = (4,6))
ax1 = fig.add_subplot(gs[:-1,:]) ##for the plot
ax2 = fig.add_subplot(gs[-1,:])   ##for the legend

ax1.imshow(array)

legend_data =[
[
        127,
        [
            100,
            100,
            100
        ],
        u"无数据区"
    ],
...
]
handles = [
    Rectangle((0,0),1,1, color = tuple((v/255 for v in c))) for k,c,n in legend_data
]
labels = [n for k,c,n in legend_data]

ax2.legend(handles,labels, mode='expand', ncol=3, prop=ChineseFont)
ax2.axis('off')
plt.show()
这看起来像这样:

EDIT2


我找到了一种方法来正确显示汉字的帮助。这个例子现在应该在Python2.7和Python3.5中使用——只需在每个标签前面加上一个
u
,然后除以
255.0
,而不只是
255
,我做了一些修改,使它更实用

一般来说,我们不知道照片上总共有哪些颜色

因此,我使用
KMeans
对图像颜色进行分类

from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
import matplotlib
import numpy as np
import pandas as pd

matplotlib.rcParams['font.sans-serif'] = ['MingLiu']  # 細明體

y = x = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, y)
array = np.sin(X) * np.cos(Y)
USING_ONLINE_IMAGE_FLAG = input('load image from online?(y/n)').upper() == 'Y'
if USING_ONLINE_IMAGE_FLAG:  # 'meteorological.png'
    from sklearn import cluster
    from urllib.request import urlopen
    from PIL import Image

    USING_ONLINE_IMAGE_FLAG = True

    # read image from online
    url_meteorological_img = "https://i.stack.imgur.com/OOIw5.png"
    image = Image.open(urlopen(url_meteorological_img))  # imageio.imread('meteorological.png', as_gray=False, pilmode="RGB")
    image = np.asarray(image)
    x, y, z = image.shape
    image_2d = image.reshape(x * y, z)

    # cluster different regions of the image.
    kmeans_cluster = cluster.KMeans(n_clusters=10)
    kmeans_cluster.fit(image_2d)
    cluster_centers = kmeans_cluster.cluster_centers_
    cluster_labels = kmeans_cluster.labels_
    picture_data = cluster_centers[cluster_labels].reshape(x, y, z).astype(int)
    cluster_colors = set(map(tuple, picture_data.reshape(-1, 3)))
    print(cluster_colors)
    array = picture_data

df_legend = pd.DataFrame([[127, [100, 100, 100], '无数据区'],
                          [126, [0, 0, 0], '外太空'],
                          [0, [200, 255, 255], '晴空'],
                          [2, [0, 0, 244], '水云'],
                          [3, [32, 165, 225], '过冷水云'],
                          [4, [33, 255, 170], '混合云'],
                          [5, [255, 0, 0], '厚冰云'],
                          [6, [180, 20, 255], '卷云'],
                          [7, [105, 255, 0], '多层云'],
                          [9, [224, 180, 0], '不确定']],
                         columns=['key', 'color', 'name'])

handles_1 = [Rectangle((0, 0), 1, 1, color=[c / 255 for c in color_list]) for color_list in df_legend['color']]
handles_2 = [Rectangle((0, 0), 1, 1, color=[c / 255 for c in color_list]) for color_list in cluster_colors] if USING_ONLINE_IMAGE_FLAG else None
labels = df_legend['name']
plt.figure(figsize=(3, 1))
plt.subplots_adjust(hspace=0)  # plt.tight_layout()
plt.rcParams.update({'legend.fontsize': 26})
plt.rc(('xtick', 'ytick'), color=(1, 1, 1, 0))
plt.subplot(3, 1, 1), plt.imshow(array, aspect='auto')
plt.subplot(3, 1, 2), plt.legend(handles_1, labels, mode='expand', ncol=3)
plt.subplot(3, 1, 3), plt.legend(handles_2, cluster_colors, mode='expand', ncol=3) if USING_ONLINE_IMAGE_FLAG else None
plt.show()

演示:

欢迎使用堆栈溢出!不幸的是,根据您提供的信息,很难帮助您。请看。简而言之,请向我们提供用于生成图像和所有相关标记的代码(我猜至少缺少
matplotlib
标记。显示您的尝试。非常感谢!但我收到了错误“ValueError:to_rgba:Invalid rgba arg”“类型为“generator”的对象没有len()”at'矩形((0,0),1,1,颜色)=(v/255表示c中的v)表示图例_data'中的k、c、n,如果可以将图例放在图像下?@Dawn您使用的是什么版本的python?我使用python 3.5我使用python 2。7@Dawn好的,尝试用
color=[v/255表示c中的v]替换
color=(v/255表示c中的v)
,即用括号替换括号。@Dawn或
color=tuple((v/255表示c中的v))
不幸的是,我没有为python2.7安装matplotlib,因此我无法自己测试它。