python - 如何在同一绘图上使用seaborn中的多个颜色图

标签 python matplotlib seaborn

我有一些测试数据:

import numpy as np
x_data = np.arange(10)
y = np.random.rand(len(x_data))

具有不同的属性

ix1 = x_data < 5
ix2 = x_data >= 5

我想从视觉上研究差异,但我搞乱了情节:

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_context('poster')

fig, ax = plt.subplots(figsize=(4, 4))
for i, x in enumerate(x_data):
    if ix1[i]:
        sns.set_palette('rainbow', sum(ix1))
    if ix2[i]:
        sns.set_palette('coolwarm', sum(ix2))
    plt.plot(x, y[i], 'o', label='{}'.format(x))
plt.legend(loc='best', prop={'size': 6})
plt.show()

结果应该是点 0-4 是彩虹色(红紫色),点 5-9 是冷暖色(蓝白红),但是:

seaborn output

那么,有两个问题:

  1. 调用plt.subplots之后再调用sns.set_palette()可以吗?
  2. 有没有办法多次设置调色板?

最佳答案

不,由于 matplotlib 的工作方式,调色板是 Axes 对象的属性,因此无论当前设置的调色板是什么,Axes 都是创建的就是它将要使用的。如果您想破解私有(private)属性,这是可以解决的(请参阅 here ),但我真的不建议这样做。

以下是我可以针对您的情况提出的建议,使用可能不广泛适用的稍微不同的方法:

pal1 = sns.color_palette('rainbow', sum(ix1))
pal2 = sns.color_palette('coolwarm', sum(ix2))

fig, ax = plt.subplots(figsize=(4, 4))
ax.scatter(x_data[ix1], y[ix1], c=pal1, s=60, label="smaller")
ax.scatter(x_data[ix2], y[ix2], c=pal2, s=60, label="larger")
ax.legend(loc="lower right", scatterpoints=5)

enter image description here

FWIW,这种可视化感觉非常复杂且难以处理(并且您选择的两个调色板有相当多的重叠,并且并不真正适合这些数据),因此可能值得从更简单的东西开始。

关于python - 如何在同一绘图上使用seaborn中的多个颜色图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25894800/

相关文章:

python - 如何使用 matplotlib 绘制一维高斯混合模型的 pdf

python - 我如何指定彩虹配色方案应如何转换为灰度

Python Pycharm : Plot gets Coarse When Zoomed in

python - 可视化两个数值数组之间的差异

python - 从 Python 脚本中读取 systemd 日志

python - 使用 Python 向远程进程发送信号

python - Pandas DataFrame.hist Seaborn 等价物

python - 在seaborn.lineplot中连接闭环?

python - 捕获由 python 子进程创建的进程的运行时错误

python - 生成随机且唯一的索引,其范围为 n 个组合