简体   繁体   English

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

[英]Extreme Basics: Displaying an Array as an Image (2d or 3d)

I'm completely new to Numpy, and I have not used Python in almost 8 years [so it's best to assume I'm completely new to Python, too]. 我是Numpy的新手 ,并且已经有近8年没有使用Python了[因此最好假设我也是Python的新手]。

I am trying to display a 2-dimensional array as a colorful image. 我正在尝试将二维数组显示为彩色图像。 I would like to be able to do this with 3-dimensional arrays, too. 我也希望能够使用3维数组来做到这一点。

For context, I would like to display this array ( array with letters ) with colors like this: ( array with colors ) 对于上下文,我想用以下颜色显示此数组( 带有字母的数组 ):( 带有颜色的数组

Step two would be to be able to make a rotatable figure that would show a 3d array (essentially like array 1 above but with an extra ABCD dimension, making triples like ABC, ABD, ACD, etc. instead of pairs like AB, AC, AD etc.). 第二步将是制作一个可旋转的图形,该图形将显示一个3d数组(基本上与上面的数组1类似,但具有额外的ABCD尺寸,使之成为ABC,ABD,ACD等三元组,而不是AB,AC,广告等)。

Important to note: I am not completely certain whether I understood your request right; 需要注意的:我不是完全肯定我是否了解您的要求; hence I'll give a answer first, which covers your question only partly. 因此,我将首先给出一个答案,其中仅部分涵盖了您的问题。 I'll improve this later, once you let me know I'm going in the right direction. 一旦您让我知道我朝着正确的方向前进,我将对此进行改进。

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

In response to Asmus: This is a really good start. 回应Asmus:这是一个非常好的开始。 I tried to take your code and modify it, but unforunately something went wrong. 我试图获取您的代码并对其进行修改,但不幸的是出了点问题。 Some of the cells are not the colors I'm telling them to be. 有些单元格不是我要告诉它们的颜色。

I'm also curious if it's possible to make a 3-dimensional version of this. 我也很好奇是否可以制作3维版本。 This would have 3-d cells of different colors, each cell representing a different 3-letter permutation, where two cells have the same color if they contain the exact same 3 letters. 这将具有不同颜色的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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM