极端基础知识:将数组显示为图像(2d或3d)

时间:2019-04-13 00:13:13

标签: python-3.x image numpy matplotlib multidimensional-array

我对Numpy来说是完全,而且我已经有近八年没有使用Python了[所以最好假设我也是Python的新手]。

我正在尝试将二维数组显示为彩色图像。我也希望能够使用3维数组来做到这一点。

对于上下文,我想显示此数组 (array with letters) 具有以下颜色: (array with colors

第二步将是制作一个可旋转的图形,该图形将显示一个3d数组(本质上类似于上面的数组1,但具有额外的ABCD尺寸,而是制作三元组,例如ABC,ABD,ACD等)。对,例如AB,AC,AD等)。

2 个答案:

答案 0 :(得分:0)

重要说明:我不确定您是否完全理解您的请求;因此,我将首先给出一个答案,其中仅部分涵盖了您的问题。一旦您让我知道我朝着正确的方向前进,我将在稍后对此进行改进。

#!/usr/bin/env python3

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap

### create a dictionary with:
#   dictionary keys: will be used as color index
#   "name": "AB" <-- the text value
#   "color": "#ff0000" <-- a hex color value to represent each square
#   "coordinate": [1,0] <-- coordinates as x in (0,1,2) 
#                           and y-index (0,1) : where to plot this entry
dataDict={
    0:{"name":"AA",
    "color":"#ff0000",
    "coordinate":[0,0]},
    1:{"name":"AB",
    "color":"#00ff00",
    "coordinate":[1,0]},
    2:{"name":"AC",
    "color":"#0000ff",
    "coordinate":[2,0]},
    3:{"name":"BA",
    "color":"#0fff0f",
    "coordinate":[0,1]},
    4:{"name":"BB",
    "color":"#cecece",
    "coordinate":[1,1]},
    5:{"name":"BC",
    "color":"#000033",
    "coordinate":[2,1]},
}

### define the size of your array in x- and y-direction
x_size=3
y_size=2

### create an empty image array of proper dimensions
img_array = np.zeros((y_size,x_size))

### iterate over the dictionary: 
#   - looking up the color index (0-5)
#   - and store it in the img_array
for i,v in dataDict.items():
    [xi,yi]=v["coordinate"]
    img_array[yi,xi] = i

### create a colormap which 
#   maps the dictionary keys (0-5) to the respective color value
cmap = ListedColormap([v["color"] for i,v in dataDict.items()])

### create a figure and subplot
fig,ax=plt.subplots(1,1)
### tell the subplot to show the image "img_array" using the colormap "cmap"
ax.imshow(img_array,cmap=cmap,zorder=1,origin="upper")

#### iterate over the dictionary, get the coordiantes and names, and place text
for i,v in dataDict.items():
    print(i,v["coordinate"][0],v["coordinate"][1])
    ax.text(v["coordinate"][0],v["coordinate"][1],v["name"],zorder=2,)

### shwo the plot    
plt.show()

答案 1 :(得分:0)

针对Asmus: 这是一个很好的开始。我试图获取您的代码并对其进行修改,但不幸的是出了点问题。有些单元格不是我要告诉它们的颜色。

我也很好奇是否可以制作3维版本。这将具有不同颜色的3-d单元,每个单元代表一个不同的3字母排列,如果两个单元包含完全相同的3个字母,则两个单元将具有相同的颜色。

#!/usr/bin/env python3

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap

### create a dictionary with:
#   dictionary keys: will be used as color index
#   "name": "AB" <-- the text value
#   "color": "#ff0000" <-- a hex color value to represent each square
#   "coordinate": [1,0] <-- coordinates as x in (0,1,2) 
#                           and y-index (0,1) : where to plot this entry

white = "#ffffff"
grey = "#cecece"
red = "#ff0000"
green = "#00ff00"
purple = "#ccbbee"
pink = "#ffaabb"
dataDict={
    1:{"name":"A",
    "color":white,
    "coordinate": [0, 1]},
    2:{"name":"B",
    "color":white,
    "coordinate": [0, 2]},
    3:{"name":"C",
    "color":white,
    "coordinate": [0, 3]},
    4:{"name":"A",
    "color":white,
    "coordinate": [1, 0]},
    5:{"name":"B",
    "color":white,
    "coordinate": [2, 0]},
    6:{"name":"C",
    "color":white,
    "coordinate": [3, 0]},

    7:{"name":"AA",
    "color":grey,
    "coordinate":[1,1]},

    8:{"name":"AB",
    "color":green,
    "coordinate":[2,1]},

    9:{"name":"AC",
    "color":pink,
    "coordinate":[3,1]},

    10:{"name":"BA",
    "color":green,
    "coordinate":[1,2]},

    11:{"name":"BB",
    "color":grey,
    "coordinate":[2,2]},

    12:{"name":"BC",
    "color":purple,
    "coordinate":[3,2]},

    13:{"name":"CA",
    "color":pink,
    "coordinate":[1,3]},

    14:{"name":"CB",
    "color":purple,
    "coordinate":[2,3]},

    15:{"name":"CC",
    "color":grey,
    "coordinate":[3,3]}
}

### define the size of your array in x- and y-direction
x_size=4
y_size=4

### create an empty image array of proper dimensions
img_array = np.zeros((y_size,x_size))

### iterate over the dictionary: 
#   - looking up the color index (0-5)
#   - and store it in the img_array
for i,v in dataDict.items():
    [xi,yi]=v["coordinate"]
    img_array[yi,xi] = i

### create a colormap which 
#   maps the dictionary keys (0-5) to the respective color value
cmap = ListedColormap([v["color"] for i,v in dataDict.items()])

### create a figure and subplot
fig,ax=plt.subplots(1,1)
### tell the subplot to show the image "img_array" using the colormap "cmap"
ax.imshow(img_array,cmap=cmap,zorder=1,origin="upper")

#### iterate over the dictionary, get the coordiantes and names, and place text
for i,v in dataDict.items():
    print(i,v["coordinate"][0],v["coordinate"][1])
    ax.text(v["coordinate"][0],v["coordinate"][1],v["name"],zorder=2,)

### shwo the plot    
plt.show()