1
0

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 1 year has passed since last update.

色空間上のL1距離を可視化する

Posted at

matplotlibで色空間上のL1距離を可視化してプロットする

関数

L1距離をプロットする関数

def plot_L1(original, input_data, plot_color):
  plt.plot([original[0], input_data[0]], [original[1], original[1]], [original[2] , original[2]], c=plot_color,ls=":")
  plt.plot([input_data[0], input_data[0]], [original[1], input_data[1]], [original[2] , original[2]], c=plot_color,ls=":")
  plt.plot([input_data[0], input_data[0]], [input_data[1], input_data[1]], [original[2] , input_data[2]], c=plot_color,ls=":")

カラーコードを作成する関数

def make_color_code(input_data):
  R_16  = '{:02x}'.format(int(input_data[0] * 255), 'x')
  G_16  = '{:02x}'.format(int(input_data[1] * 255), 'x')
  B_16  = '{:02x}'.format(int(input_data[2] * 255), 'x')
  color_str ="#" + R_16 + G_16 + B_16
  return color_str

実行プログラム

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation



#データの作成
original = np.random.rand(3)
new_data_1 = np.random.rand(3)
new_data_2 = np.random.rand(3)


# figureを作成
fig = plt.figure(figsize = (8, 8))
ax = fig.add_subplot(111, projection='3d')

#figureの設定
ax.view_init(elev=30, azim=30)
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_zlim([0, 1])
ax.tick_params(bottom=False, left=False, right=False, top=False)
ax.tick_params(labelbottom=False, labelleft=False, labelright=False, labeltop=False)

#記述
ax.scatter(original[0], original[1], original[2], c="white", edgecolors="black", s=200 ,alpha=1)

plot_L1(original, new_data_1, "red")
plot_L1(original, new_data_2, "blue")

color_code_1 = make_color_code(new_data_1)
color_code_2 = make_color_code(new_data_2)

ax.scatter(new_data_1[0], new_data_1[1], new_data_1[2], c=f"{color_code_1}", edgecolors="black", s=100 ,alpha=1)
ax.scatter(new_data_2[0], new_data_2[1], new_data_2[2], c=f"{color_code_2}", edgecolors="black", s=100 ,alpha=1)

実行結果

ダウンロード (57).png

おまけ

これを作りたかっただけ...

image (1).gif

1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?