如何使用连续值[`seaborn`调色板?'为`matplotlib`散点图着色?

时间:2016-09-27 22:11:41

标签: matplotlib plot colors gradient seaborn

我有一个散点图,我想根据另一个值(在这种情况下天真地分配给np.random.random())对其进行着色。

有没有办法使用seaborn将每个点的连续值(与绘制的数据没有直接关联)映射到seaborn中连续渐变的值?< / strong>

这是我生成数据的代码:

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

但它不喜欢连续值。

我想使用像:

这样的调色板
sns.palplot(sns.cubehelix_palette(8))

enter image description here

我也试过做类似下面的事情,但它没有意义b / c它不知道我在上面的cmap字典中使用了哪些值:

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

2 个答案:

答案 0 :(得分:23)

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

答案 1 :(得分:1)

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