如何在同一绘图的seaborn中使用多个颜色图 [英] How to use multiple colormaps in seaborn on same plot

查看:31
本文介绍了如何在同一绘图的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

我想从视觉上调查差异,但是弄糟了这个图:

I want to investigate the differences visually, but am messing the plot up:

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 是冷暖色(蓝白红),但是:

The result should be points 0-4 are rainbow (red-violet) and points 5-9 are coolwarm (blue-white-red), but instead:

那么,两个问题:

  1. 在调用 plt.subplots 后可以调用 sns.set_palette()吗?
  2. 是否可以多次设置调色板?

推荐答案

不,由于 matplotlib 的工作方式,调色板是 Axes 对象的一个​​属性,因此无论当前设置的调色板如何是在创建 Axes 时使用的.如果您想破解私有属性,这是可以绕过的(请参阅此处),但我不建议这么做.

No, because of the way matplotlib works, the color palette is a property of the Axes object and so whatever the currently set palette is at the time an Axes is created is what it's going to use. This is possible to get around if you want to hack on private attributes (see here), but I wouldn't really recommend that.

这是我可以针对您的情况提出的方法,它使用了一种可能不太适用的稍微不同的方法:

Here's what I could come up with in your case, using a somewhat different approach that might not be broadly applicable:

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)

FWIW,这种可视化感觉非常复杂且难以处理(并且您选择的两个调色板重叠了很多,并不真正适用于这些数据),因此可能值得从更简单的角度入手.

FWIW, this visualization feels pretty complex and hard to process (and the two palettes you've chosen overlap a fair amount and aren't really appropriate for these data) so it might be worth starting with something simpler.

这篇关于如何在同一绘图的seaborn中使用多个颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆