2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

光の3原色の勉強ついでに3次元RGBプロットしてみた

Last updated at Posted at 2021-05-23

概要

色に興味を持ちまして、RGBの様々な組み合わせの色をパッと把握する図を作りたいなーとググってたら、下記発見しまして

https://stackoverflow.com/questions/40853556/3d-discrete-heatmap-in-matplotlib
image.png

「何これカッコいい!」といてもたってもいられず、3次元プロットをやってみました
(すでに目的がずれている)

これを参考にplot_surfaceを使ったんですが、くすんだ色になってしまい、これが解決できず・・。
cmap使えばきれいな色出てそうなんですが、colorやfacecolorで指定するとダメでした)

諦めて最後はscatterに落ち着いたのが悲しいところですが、何かの役に立てばとコードを残しておきます
(徒労に終わった感を少しでも払拭したい・・・)

plot_surface

image.png

colorでRGBA指定すると、くすんだ色になってしまいました。
例えば、右下のところは[0.9, 0.9, 0, 1]なので黄色になるはずなんですが、めっちゃ暗い色になってるんですよね。。。(丸くてきれいな黄色は、確認用の普通のplotです)

    import matplotlib.pyplot as plt
    import numpy as np

    plt.close(1)
    fig = plt.figure(1, figsize=(12, 8))
    ax = fig.add_subplot(1, 1, 1, projection="3d")

    ystep = 1 / 10
    zstep = 1 / 10
    Y = np.arange(0, 1, ystep)
    Z = np.arange(0, 1, zstep)
    alpha = 1
    for x in [0.0, 0.5, 1]:
        for y in Y:
            for z in Z:
                color = [x, y, z, alpha]
                y1 = y
                y2 = y + ystep
                z1 = z
                z2 = z + zstep
                y_mesh, z_mesh = np.meshgrid([y1,y2], [z1, z2])
                x_for_yz = np.ones_like(y_mesh) * x
                
                surf = ax.plot_surface(    # くすんだ色になる 解決できず
                    X=x_for_yz,
                    Y=y_mesh, 
                    Z=z_mesh,
                    color=color,
                    linewidth=0, 
                    antialiased=False
                )
    ax.set_xlabel('x: Red', color='red')
    ax.set_ylabel('y: Green', color='green')
    ax.set_zlabel('z: Blue', color='blue')
    ax.tick_params(axis='x', color='red', labelcolor='red')
    ax.tick_params(axis='y', color='green', labelcolor='green')
    ax.tick_params(axis='z', color='blue', labelcolor='blue')

    # こっちだときれいな発色
    ax.plot([1], [1], marker='o', markersize=20,
        color=[1, 1, 0, alpha], )
    ax.plot([1], [0], marker='o', markersize=20,
        color=[1, 0, 0, alpha], )
    ax.plot([1], [0.6], marker='o', markersize=20,
        color=[1, 0.6, 0, alpha], )

scatter

x軸:Red
image.png

x軸:Green
image.png

x軸:Blue
image.png

こっちはきれいな色出てますね

    import matplotlib.pyplot as plt
    import numpy as np
    
    dct = {0:0, 1:1, 2:2}  # x: red
#    dct = {0:2, 1:0, 2:1}  # x: green
#    dct = {0:1, 1:2, 2:0}  # x: blue

    plt.close(1)
    fig = plt.figure(1, figsize=(12, 8))
    ax = fig.add_subplot(1, 1, 1, projection="3d")
    Y = np.arange(0, 1, 1 / 10)
    Z = np.arange(0, 1, 1 / 10)
    alpha = 1
    for x in [0.0, 0.5, 1]:
        for y in Y:
            for z in Z:
                rgba = [x, y, z, alpha]
                color = [rgba[dct[0]], rgba[dct[1]], rgba[dct[2]], rgba[3]]
                ax.scatter(x, y, z,
                    color=color, marker='s', s=20*4
                    )
    xcolor = [0, 0, 0, 1]
    ycolor = [0, 0, 0, 1]
    zcolor = [0, 0, 0, 1]
    for i in [0, 1, 2]:
        if dct[i]==0: xcolor[i] = 1
        if dct[i]==1: ycolor[i] = 1
        if dct[i]==2: zcolor[i] = 1
            
    ax.set_xlabel('x', color=xcolor)
    ax.set_ylabel('y', color=ycolor)
    ax.set_zlabel('z', color=zcolor)
    ax.tick_params(axis='x', color=xcolor, labelcolor=xcolor)
    ax.tick_params(axis='y', color=ycolor, labelcolor=ycolor)
    ax.tick_params(axis='z', color=zcolor, labelcolor=zcolor)

終わり

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?