<jupyter_start><jupyter_text>5.6: 色の設定<jupyter_code># リスト 5.6.1　色の設定

import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline
x = np.arange(0, 6, 0.1)
y = np.sin(x)
z = np.cos(x)
fig, ax = plt.subplots(1, 1)

# 描画
ax.scatter(x, y, label="sin", color="lightblue", edgecolor="blue")
ax.scatter(x, z, label="cos", color="lightblue", edgecolor="blue", alpha=0.3)
ax.legend(loc="lower left")
# リスト 5.6.2　色指定の例

fig, ax = plt.subplots(1, 1)
v = np.array([0, 1])

# 各種方法による色指定で描画
ax.plot(v, v, color="k", label="color string", linewidth=8)
ax.plot(v + 1, v, color="0.5", label="Gray scale", linewidth=8)
ax.plot(v + 2, v, color=(0, 0, 0.7, 1), label="RGBA tuple", linewidth=8)
ax.plot(v + 3, v, color="#66b3e9", label="RGBA 16", linewidth=8)
ax.plot(v + 4, v, color="DarkGreen", label="HTML color", linewidth=8)
ax.plot(v + 5, v, color="LimeGreen", label="X11/CSS", linewidth=8)
ax.plot(v + 6, v, color="C3", label="Regular expression", linewidth=8)
ax.plot(v + 7, v, color="tab:pink", label="Tableau", linewidth=8)
ax.legend(loc="upper left", bbox_to_anchor=(1, 1))
# リスト 5.6.3　カラーマップを取得して色を設定

import matplotlib.cm as cm

fig, ax = plt.subplots(1, 1)
cname = "viridis"
n = 10
cmap = cm.get_cmap(cname, n)
for i in range(n):
    ax.plot(x, y + n - i, color=cmap(i), label=f"{cname}({i})")

ax.legend(loc="upper left", bbox_to_anchor=(1, 1))
# リスト 5.6.4　カラーマップを取得して色を設定

fig, ax = plt.subplots(1, 1)
cmap = cm.viridis
for i in np.linspace(0, 1, 10):
    ax.plot(x, y + 2 - i, color=cmap(i), label=f"viridis({i:.1f})")

ax.legend(loc="upper left", bbox_to_anchor=(1, 1))
# リスト 5.6.5　段階的に色を変化させた散布図

fig, ax = plt.subplots(1, 1)
ax.scatter(x, y, c=y, cmap=cm.Blues)
# リスト 5.6.6　個別に色を指定した散布図

fig, ax = plt.subplots(1, 1)
x = [0, 1]
y = np.array([0.4, 0.4])
cmap = cm.get_cmap("viridis")
ax.plot(x, y, color=cmap(0.0), label="cmap(0.0)")
ax.plot(x, y - 0.2, color=cmap(0.6), label="cmap(0.6)")
ax.set_ylim(0, 0.6)
ax.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2163265306122449, 'fraction_numerical': 0.053061224489795916, 'mean_word_length': 4.906626506024097, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '408184', 'license': 'MIT', 'n_tokens_mistral': 1053, 'n_tokens_neox': 998, 'n_words': 207, 'path': 'notebooks/5-06.ipynb', 'repo_name': 'yohata1013/sample-code-2nd', 'size': '1960'}