【问题标题】:Extreme Basics: Displaying an Array as an Image (2d or 3d)极端基础:将数组显示为图像(2d 或 3d)
【发布时间】:2019-09-03 18:21:21
【问题描述】:

对 Numpy 完全陌生,而且我已经将近 8 年没有使用 Python [所以最好假设我对 Python 也完全陌生]。

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

对于上下文,我想显示这个数组 (array with letters) 像这样的颜色: (array with colors)

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

【问题讨论】:

  • 字母和颜色的对应关系是怎样的呢?为什么A单独是某种颜色而AB/AC/AD都是相同的颜色?请提供足够的信息以便任何人能够帮助您

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


【解决方案1】:

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

#!/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()

【讨论】:

  • 嗨,阿斯穆斯。感谢你的回复。我为我的问题发布了一个“答案”以回复您的回答,只是因为我的回复太长,无法发表评论。
【解决方案2】:

回应阿斯穆斯: 这是一个非常好的开始。我试图获取您的代码并对其进行修改,但不幸的是出了点问题。有些单元格不是我告诉他们的颜色。

我也很好奇是否可以制作这个的 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2011-09-11
    相关资源
    最近更新 更多