我想创建一个条形图,使条形的颜色与以前绘制的散点图的颜色相对应。这是一个示例:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
array = np.random.rand(100,2)
kmeans = KMeans(n_clusters=10)
kmeans.fit(array)
y_kmeans = kmeans.predict(array)
y_kmeans_df = pd.DataFrame(y_kmeans)
y_kmeans_df.columns = ['Values']
y_kmeans_df.Values.value_counts(normalize=True)
fig, (ax0,ax1) = plt.subplots(2,1,figsize=(10,15))
ax0.scatter(array[:,0], array[:,1], c=y_kmeans, s=50, cmap='viridis')
ax1.bar(y_kmeans_df.Values.value_counts(sort=False).index ,
y_kmeans_df.Values.value_counts(normalize=True, sort=True).values)
ax1.set_xticks(y_kmeans_df.Values.value_counts(sort=False).index)
ax1.set_xticklabels(y_kmeans_df.Values.value_counts(sort=True).index)
plt.show()
因此,在这种情况下,使用matplotlib,我希望条形颜色与其对应的簇的颜色相同。我不确定如何实现。