実現したいこと
・数字画像データがどのようなものなのかイメージするため、次元削減を
使って3Dグラフで表示したい。(テキストのこのLESSONで実現しようとし
ていること)
前提
テキストの指示に従いコードを入力しました。
その結果、以下のエラーが表示されます。
ValueError: 'color' kwarg must be a color or sequence of color specs. For a sequence of values to be color-mapped, use the 'c' argument instead.
発生している問題・エラーメッセージ
以下のとおりValueErrorが表示されます。
エラーメッセージ
ValueError Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\matplotlib\axes_axes.py:4378, in Axes._parse_scatter_color_args(c, edgecolors, kwargs, xsize, get_next_color_func)
4377 try:
-> 4378 mcolors.to_rgba_array(kwcolor)
4379 except ValueError as err:
File ~\anaconda3\lib\site-packages\matplotlib\colors.py:485, in to_rgba_array(c, alpha)
484 else:
--> 485 rgba = np.array([to_rgba(cc) for cc in c])
486 else:
File ~\anaconda3\lib\site-packages\matplotlib\colors.py:485, in <listcomp>(.0)
484 else:
--> 485 rgba = np.array([to_rgba(cc) for cc in c])
486 else:
File ~\anaconda3\lib\site-packages\matplotlib\colors.py:299, in to_rgba(c, alpha)
298 if rgba is None: # Suppress exception chaining of cache lookup failure.
--> 299 rgba = _to_rgba_no_colorcycle(c, alpha)
300 try:
File ~\anaconda3\lib\site-packages\matplotlib\colors.py:374, in _to_rgba_no_colorcycle(c, alpha)
373 return c, c, c, alpha if alpha is not None else 1.
--> 374 raise ValueError(f"Invalid RGBA argument: {orig_c!r}")
375 # turn 2-D array into 1-D array
ValueError: Invalid RGBA argument: 'GLAY'
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
Cell In[20], line 5
3 ax = fig.add_subplot(projection='3d')
4 # 各点の数字に対する色で散布図を描画
----> 5 ax.scatter(df[0], df[1], df[2], color=colors)
7 # 数字がどの色か見本を描画
8 ty = 0
File ~\anaconda3\lib\site-packages\matplotlib_init_.py:1442, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
1439 @functools.wraps(func)
1440 def inner(ax, *args, data=None, **kwargs):
1441 if data is None:
-> 1442 return func(ax, *map(sanitize_sequence, args), **kwargs)
1444 bound = new_sig.bind(ax, *args, **kwargs)
1445 auto_label = (bound.arguments.get(label_namer)
1446 or bound.kwargs.get(label_namer))
File ~\anaconda3\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py:2275, in Axes3D.scatter(self, xs, ys, zs, zdir, s, c, depthshade, *args, **kwargs)
2272 if np.may_share_memory(zs_orig, zs): # Avoid unnecessary copies.
2273 zs = zs.copy()
-> 2275 patches = super().scatter(xs, ys, s=s, c=c, *args, **kwargs)
2276 art3d.patch_collection_2d_to_3d(patches, zs=zs, zdir=zdir,
2277 depthshade=depthshade)
2279 if self._zmargin < 0.05 and xs.size > 0:
File ~\anaconda3\lib\site-packages\matplotlib_init_.py:1442, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
1439 @functools.wraps(func)
1440 def inner(ax, *args, data=None, **kwargs):
1441 if data is None:
-> 1442 return func(ax, *map(sanitize_sequence, args), **kwargs)
1444 bound = new_sig.bind(ax, *args, **kwargs)
1445 auto_label = (bound.arguments.get(label_namer)
1446 or bound.kwargs.get(label_namer))
File ~\anaconda3\lib\site-packages\matplotlib\axes_axes.py:4602, in Axes.scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite, **kwargs)
4599 if edgecolors is None:
4600 orig_edgecolor = kwargs.get('edgecolor', None)
4601 c, colors, edgecolors =
-> 4602 self._parse_scatter_color_args(
4603 c, edgecolors, kwargs, x.size,
4604 get_next_color_func=self._get_patches_for_fill.get_next_color)
4606 if plotnonfinite and colors is None:
4607 c = np.ma.masked_invalid(c)
File ~\anaconda3\lib\site-packages\matplotlib\axes_axes.py:4380, in Axes._parse_scatter_color_args(c, edgecolors, kwargs, xsize, get_next_color_func)
4378 mcolors.to_rgba_array(kwcolor)
4379 except ValueError as err:
-> 4380 raise ValueError(
4381 "'color' kwarg must be a color or sequence of color "
4382 "specs. For a sequence of values to be color-mapped, use "
4383 "the 'c' argument instead.") from err
4384 if edgecolors is None:
4385 edgecolors = kwcolor
ValueError: 'color' kwarg must be a color or sequence of color specs. For a sequence of values to be color-mapped, use the 'c' argument instead.
該当のソースコード
ソースコード
from sklearn.datasets import load_digits
from sklearn import decomposition
from mpl_toolkits.mplot3d import Axes3D
digits = load_digits()
X = digits.data
y = digits.target
#0~9の色名を用意する
numbercolor = ["BLACK","BROWN","RED","DARKORANGE","GOLD","GREEN","BLUE","PURPLE","GLAY","SKYBLUE"]
# yの値を色名に変えて、colorsリストを作る
colors = []
for i in y:
colors.append(numbercolor[i])
# 主成分分析で、64個の特徴量を3個へと次元を減らす
pca = decomposition.PCA(n_components=3)
feature3 = pca.fit_transform(X)
# 3個へ減らしたデータ(features3)で、データフレームを作る
df = pd.DataFrame(feature3)
# 3D散布図の準備
fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(projection='3d')
# 3個の特徴量をX,Y,Zにして、各点の数字に対する色で散布図を描画
ax.scatter(df[0], df[1], df[2], color=colors)
# 数字がどの色かの見本を描画
ty = 0
for col in numbercolor:
ax.text(50, 30, 30-ty*5, str(ty), size=20, color=col)
ty+=1
plt.show
試したこと
上記エラーでGoogleで検索して表示される対処方法は試してみましたが
が改善しませんでした。
補足情報(FW/ツールのバージョンなど)
バージョン Python 3.6.1

回答1件
あなたの回答
tips
プレビュー