2016-09-27 468 views
4

我有散點圖,我想根據另一個值(在本例中天真地分配給np.random.random())對它進行着色。如何使用連續值[`seaborn`調色板?]顏色`matplotlib` scatterplot

是否有一種方法可以使用seaborn將每個點的連續值(與繪製的數據不直接關聯)映射爲seaborn中連續梯度的值?

這裏是我的代碼來生成數據:

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
from sklearn.datasets import load_iris 
from sklearn.preprocessing import StandardScaler 
from sklearn import decomposition 
import seaborn as sns; sns.set_style("whitegrid", {'axes.grid' : False}) 

%matplotlib inline 
np.random.seed(0) 

# Iris dataset 
DF_data = pd.DataFrame(load_iris().data, 
         index = ["iris_%d" % i for i in range(load_iris().data.shape[0])], 
         columns = load_iris().feature_names) 

Se_targets = pd.Series(load_iris().target, 
         index = ["iris_%d" % i for i in range(load_iris().data.shape[0])], 
         name = "Species") 

# Scaling mean = 0, var = 1 
DF_standard = pd.DataFrame(StandardScaler().fit_transform(DF_data), 
          index = DF_data.index, 
          columns = DF_data.columns) 

# Sklearn for Principal Componenet Analysis 
# Dims 
m = DF_standard.shape[1] 
K = 2 

# PCA (How I tend to set it up) 
Mod_PCA = decomposition.PCA(n_components=m) 
DF_PCA = pd.DataFrame(Mod_PCA.fit_transform(DF_standard), 
         columns=["PC%d" % k for k in range(1,m + 1)]).iloc[:,:K] 
# Plot 
fig, ax = plt.subplots() 
ax.scatter(x=DF_PCA["PC1"], y=DF_PCA["PC2"], color="k") 
ax.set_title("No Coloring") 

enter image description here

理想情況下,我想要做這樣的事情:

# Color classes 
cmap = {obsv_id:np.random.random() for obsv_id in DF_PCA.index} 

# Plot 



fig, ax = plt.subplots() 
ax.scatter(x=DF_PCA["PC1"], y=DF_PCA["PC2"], color=[cmap[obsv_id] for obsv_id in DF_PCA.index]) 
ax.set_title("With Coloring") 

# ValueError: to_rgba: Invalid rgba arg "0.2965562650640299" 
# to_rgb: Invalid rgb arg "0.2965562650640299" 
# cannot convert argument to rgb sequence 

,但它並沒有像連續值。

我想用一個調色板一樣:

​​

enter image description here

我也試着做一些像下面,但它是沒有意義的B/C不知道哪個值我曾經在我的字典cmap上面:

ax.scatter(x=DF_PCA["PC1"], y=DF_PCA["PC2"],cmap=sns.cubehelix_palette(as_cmap=True) 

回答

12
import numpy as np 
import seaborn as sns 
import matplotlib.pyplot as plt 

x, y, z = np.random.rand(3, 100) 
cmap = sns.cubehelix_palette(as_cmap=True) 

f, ax = plt.subplots() 
points = ax.scatter(x, y, c=z, s=50, cmap=cmap) 
f.colorbar(points) 

enter image description here

+0

也有顏色條!是。那正是我所期待的。非常感謝ヾ(⌐■_■)ノ♪@mwaskom –

+1

這很好,謝謝。有沒有一種明智的方式通過seaborn的'hue'參數來做到這一點?我嘗試過,但由於色調參數似乎將連續變量的每個值都視爲分類級別,所以產生的傳說毫無意義。 – user2428107

0
from matplotlib.cm import ScalarMappable 
from matplotlib.colors import Normalize 


cmap = {obsv_id:np.random.random() for obsv_id in DF_PCA.index} 
sm = ScalarMappable(norm=Normalize(vmin=min(list(cmap.values())), vmax=max(list(cmap.values()))), cmap=sns.cubehelix_palette(as_cmap=True)) 

# Plot 
fig, ax = plt.subplots() 
ax.scatter(x=DF_PCA["PC1"], y=DF_PCA["PC2"], color=[sm.to_rgba(cmap[obsv_id]) for obsv_id in DF_PCA.index]) 
ax.set_title("With Coloring") 

enter image description here

+0

如果任何人有,你沒有導入'ScalarMappable'和'Normalize'那麼我一定會選擇是正確的更簡單的方法。 –