Rav*_*euk 5 data-visualization python-3.x seaborn
我正在尝试使用以下代码绘制一些数据
\n\nfrom sklearn.datasets import make_blobs\nimport seaborn as sns\nimport numpy as np\n\nX, y = make_blobs(n_samples=1000, n_features=2, centers=10, cluster_std=1.0, center_box=(-10.0, 10.0), shuffle=True, random_state=None)\n\npalette = np.array(sns.color_palette("bright", 10)) #Chossing color \nsns.scatterplot(X[:,0],X[:,1],legend=\'full\',c=palette[y])\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n颜色很漂亮,但缺少图例。\n当我检查文档时,我看到:
\n\n\n\n\n如何绘制图例。如果\xe2\x80\x9cbrief\xe2\x80\x9d,数字
\nhue和size变量\n ....
所以看来我还需要包含参数hue。\n但是当我hue使用以下代码尝试参数时,会创建以下图表......
sns.scatterplot(X[:,0],X[:,1],legend=\'full\',hue=y,c=palette[y])\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n图例正在显示,但颜色不是我想要的。添加hue参数后,它似乎覆盖了调色板参数。不管我选择什么调色板,颜色仍然很难看......
我的问题是: \n如何在保持我想要的颜色的同时显示图例?
\n您需要使用palettekwarg,并用您的值指定色调y。
from sklearn.datasets import make_blobs
import seaborn as sns
import matplotlib.pyplot as plt
X, y = make_blobs(n_samples=1000, n_features=2, centers=10, cluster_std=1.0,
center_box=(-10.0, 10.0), shuffle=True, random_state=None)
palette = sns.color_palette("bright", 10) #Choosing color
sns.scatterplot(X[:, 0], X[:, 1], palette=palette, hue=y, legend='full')
plt.show()
Run Code Online (Sandbox Code Playgroud)