How to use multiple colormaps in seaborn on same plot

℡╲_俬逩灬. 提交于 2019-12-24 03:12:07

问题


I have some test data:

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

With different properties

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()

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

So, two questions:

  1. Is it ok to call sns.set_palette() after calling plt.subplots?
  2. Is there a way to set the palette more than once?

回答1:


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, 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.



来源:https://stackoverflow.com/questions/25894800/how-to-use-multiple-colormaps-in-seaborn-on-same-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!