散布図でクラスターに色を付けようとしていますが、2つの異なる方法で管理しました。
最初に各クラスターを繰り返しプロットし、2番目にすべてのデータを一度にプロットし、ラベル[0、1、2、3、4]に従ってクラスターに色を付けます。
私は私が得る結果に満足しているexample1
し、example3
代わりに繰り返し、各クラスタをプロットのラベルに応じてクラスタを着色する際着色はそう劇的に変化する理由を私は理解していません。
さらに、example1とexample3で2番目のクラスター(常に「1」のラベルが付いているにもかかわらず)の色が異なるのはなぜですか?
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight') #irrelevant here, but coherent with the examples=)
fig, ax = plt.subplots(figsize=(6,4))
for clust in range(kmeans.n_clusters):
ax.scatter(X[kmeans.labels_==clust],Y[kmeans.labels_==clust])
ax.set_title("example1")`
そして
plt.figure(figsize = (6, 4))
plt.scatter(X,Y,c=kmeans.labels_.astype(float))
plt.title("example2")
(2番目の方法のカラーマップを明示的に定義できることはわかっていますが、例1の結果を再現するものは見つかりませんでした)
これが最小限の作業例です
import matplotlib.pyplot as plt
import pandas as pd
plt.style.use('fivethirtyeight') #irrelevant here, but coherent with the examples=)
X=pd.Series([1, 2, 3, 4, 5, 11, 12, 13, 14, 15])
Y=pd.Series([1,1,1,1,1,2,2,2,2,2])
clusters=pd.Series([0,0,0,0,0,1,1,1,1,1])
fig, ax = plt.subplots(figsize=(6,4))
for clust in range(2):
ax.scatter(X[clusters==clust],Y[clusters==clust])
ax.set_title("example3")
plt.figure(figsize = (6, 4))
plt.scatter(X,Y, c=clusters)
plt.title("example4")
クラスターをループしてscatter
色を指定せずにプロットすると、アクティブなプロパティサイクラーのデフォルトの色(カラーサイクル)が使用されます。アクティブなプロパティサイクラーはで定義されていrcParams
ます。使用中のスタイルを介して設定されます。あなたの場合、'fivethirtyeight'
print(plt.rcParams["axes.prop_cycle"])
> cycler('color', ['#008fd5', '#fc4f30', '#e5ae38', '#6d904f', '#8b8b8b', '#810f7c'])
これの最初の2色( '#008fd5'、 '#fc4f30')は、プロットに表示されている色です。
ascatter
をclusters
color引数として使用すると、これらの値はカラーマップを介して色にマップされます。カラーマップが指定されていない場合は、で定義されているデフォルトのカラーマップを使用しますrcParam
。
print(plt.rcParams["image.cmap"])
> "viridis"
The 'fivethirtyeight'
style does not define any special colormap, so the default would be unchanged. (The fact that you observe a different colormap than viridis in your picture is due to the fact that there was some other code still active which is not shown in the question.)
At this point I need to start interpreting; I would think that your question really is how to get the single scatter use a colormap that has the same colors as the colorcycle in it. None of the predefined colormaps has the fivethirtyeight cycler colors in it. Hence you would define that colormap manually, by taking the colors from the cycle,
import matplotlib.colors as mcolors
cmap = mcolors.ListedColormap(plt.rcParams['axes.prop_cycle'].by_key()['color'])
Now you need a way to index the colormap, because you have discrete clusters.
n = len(clusters.unique())
norm = mcolors.BoundaryNorm(np.arange(n+1)-0.5, n)
もちろん、これには、カラーマップの色の数がクラスの数以上である必要があります。これがここに当てはまります。
すべてをまとめると(わかりやすくするために、別のカテゴリを追加しました)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.colors as mcolors
plt.style.use('fivethirtyeight') #relevant here!!
X=pd.Series([1, 2, 3, 4, 5, 11, 12, 13, 14, 15])
Y=pd.Series([1,1,1,1,1,2,2,2,2,2])
clusters=pd.Series([0,0,0,0,0,1,1,1,1,2])
cmap = mcolors.ListedColormap(plt.rcParams['axes.prop_cycle'].by_key()['color'])
n = len(clusters.unique())
norm = mcolors.BoundaryNorm(np.arange(n+1)-0.5, n)
plt.figure(figsize = (6, 4))
sc = plt.scatter(X,Y, c=clusters, cmap=cmap, norm=norm)
plt.colorbar(sc, ticks=clusters.unique())
plt.title("example4")
plt.show()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加