Pyplot study notes (1): Use scatter to draw a three-dimensional scatter plot of color images, the point coordinates are RGB values, and the point color is the color determined by the RGB value (color image color distribution map)

Preface


  Some time ago, my friend asked me to help him draw a color map of the color image. That is, the RGB value of the color image is the coordinate value, and then the color corresponding to the RGB value is used for punctuation to form a color distribution map. Pyplot is a very rich visualization tool. I took the opportunity to learn some related simple operations. I will take this note for later reference.


A glance at subplot()


  • Some parameters

  * args : The parameter to determine the position of the sub-picture, such as (2, 2, 3)=223, which means that the sub-picture will appear in the position of the third grid of the window divided into two rows and two columns.
  projection : projection method, coordinate type, such as two-dimensional rectangular coordinate system: 'rectilinear'(=default None), polar coordinate system: 'polar', three-dimensional rectangular coordinate system: '3d', earth projection: 'aitoff', 'hammer', 'lambert', 'mollweide'etc.

ax2 = plt.subplot(221, projection='rectilinear')
ax2.set_title('rectilinear')
ax2 = plt.subplot(222, projection='polar')
ax2.set_title('polar')
ax2 = plt.subplot(223, projection='3d')
ax2.set_title('3d')
ax2 = plt.subplot(224, projection='mollweide')
ax2.set_title('mollweide')
ax2.grid(True)
plt.show()

Insert picture description here

Figure 1. Projection style

  facecolor : This keyword can also be usedfcinstead. There are many ways to indicate color.
  1. RGB or RGBA tuple, tested RGB np array is also possible, the number should be limited to [0,1], where A represents transparency. Such asfc = (.18, .31, .31),fc = (0.1, 0.2, 0.5, 0.3),facecolorc=np.array([.18, .31, .31])
  2. RGB or RGBA hexadecimal string, similar to the first. For example,fc ='#0F0F0F' 、'facecolor=#0F0F0F0F'
  3. Simple characters are generally represented by the first letter of color words, and the colors that can be represented are limited, including:,'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'among themC = Cyan(青色)、M = Magenta(洋红或品红、 Y = Yellow(黄色)、K = blacK(黑色、W=white (白色).
  4. tab: color words are:'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'.
  Note: Purple, purple; Olive, brown;
   other parameters (partial) : title (Title) XLIM (coordinate range) the xlabel (identification axis) xticklabels (scale identification) XTICK (scale)


Color image color distribution map


  • Code
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv

img = cv.imread('lena.jpg')
b, g, r = cv.split(img)
img = cv.merge([r, g, b]) #为了适应fc中元组顺序为RGB,故调整
draw = np.reshape(img, (-1, 3))
lists = list(set([tuple(t) for t in draw]))  #去除列表中重复行
print(np.array(lists))
draw = tuple(draw/255)
ax = plt.subplot(1, 1, 1, projection='3d', fc=(.18, .31, .31))  # 创建一个三维绘图,并设置颜色

ax.scatter(r, g, b, s=1, color=draw)  # 以RGB值为坐标绘制数据点,并以RGB值为点颜色

ax.set_zlim(0, 255)
ax.set_ylim(0, 255)
ax.set_xlim(0, 255)
ax.set_zlabel('B', c='b')  # 坐标轴
ax.set_ylabel('G', c='g')
ax.set_xlabel('R', c='r')
ax.set_title('clolored image distribution', color='w')
ax.tick_params(labelcolor='w')
plt.show()

  • Test Results

  Pure red test picture
Insert picture description here

Figure 2. Pure red test picture

  Remove the RGB value after repeated lines: [[254 0 0]]
  draw the color distribution result
Insert picture description here

Figure 3. Color distribution of pure red test picture

  Pure red and pure green test pictures
Insert picture description here

Figure 4. Pure red and pure green test pictures

Remove the RGB value after repeated lines: It can be seen that the colors in some places are not so pure after mixing
[[ 1 254 3] [253 0 0] [0 255 1] [4 255 0] [255 1 3] [253 2 0 ] [1 255 2] [254 0 0] [1 252 0] [255 1 1] [1 255 1] [255 1 0] [254 0 2] [1 255 0] [252 0 0 [0 254 0] [253 1 0] [0 255 4] [254 1 0] [0 254 2] [254 2 1] [254 2 0] [0 255 2] [0 255 3] [253 0 2] [0 255 0] ]
Insert picture description here

Figure 5. Color distribution of pure red and pure green test pictures

  Computer vision classic image lena

Insert picture description here

Figure 6.lena

  Computer vision classic image lena color distribution

Insert picture description here

Figure 6. lena color distribution

reference


   https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot.html#matplotlib.pyplot.subplot

Guess you like

Origin blog.csdn.net/sinat_35907936/article/details/107345000