Python 使用seaborn.swarmlot将数据点映射到彩色地图

Python 使用seaborn.swarmlot将数据点映射到彩色地图,python,pandas,matplotlib,seaborn,Python,Pandas,Matplotlib,Seaborn,我想生成一个seaborn.swarmlot,其中单个数据点的颜色映射到一个颜色映射 我有一个与此类似的数据帧: In[48]:df Out[48]: a c Key 0 1 12 1st 1 4 35 2nd 2 5 12 2nd 3 6 46 1st 4 3 78 1st 5 4 45 2nd 6 5 34 1st 7 6 70 2nd 我使用以下代码生成一个Swarmlot

我想生成一个seaborn.swarmlot,其中单个数据点的颜色映射到一个颜色映射

我有一个与此类似的数据帧:

In[48]:df
Out[48]: 
      a  c   Key
   0  1  12  1st
   1  4  35  2nd
   2  5  12  2nd
   3  6  46  1st
   4  3  78  1st
   5  4  45  2nd
   6  5  34  1st
   7  6  70  2nd
我使用以下代码生成一个Swarmlot:

sns.swarmplot(x='Key', y = 'a',  s=20, data = df)
得到这样一个图:

现在,我希望数据点映射到一个colormap,它将根据数据框的列“c”表示值

我尝试在代码中添加'hue='c',并得到以下结果:

sns.swarmplot(x='Key', y = 'a',  hue='c',s=20, data = df)

这是我想要的方向,但我更喜欢将颜色映射到颜色映射,这样低的“c”值将是浅绿色,并且“c”值越高,绿色越深

作为一个传奇,我想要一个渐变

非常感谢您的帮助

不带色条的溶液 没有色条的解决方案相当简单。您需要创建一个颜色的
调色板
(颜色与值的数量相同),并使用
调色板
参数将其提供给
swarmlot

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
print sns.__version__ # swarmplot requires version 0.7.1

# Reconstruct the dataframe from the question (the hardest part)
a = [1,4,5,6,3,4,5,6]
c = [12,35,12,46,78,45,34,70]
key = [1,2,2,1,1,2,1,2]
key = ["{k}{a}".format(k=k, a={1:"st", 2:"nd"}[k]) for k in key]
df  =pd.DataFrame({"a":a, "c":c, "Key":key})

palette = sns.light_palette("seagreen", reverse=False,  n_colors=len(c) )
sns.swarmplot(x='Key', y = 'a',  hue='c',s=20, data = df, palette=palette)

plt.show()


带色条的溶液 使用colorbar的解决方案需要更多的工作。 我们需要从seaborn调色板构建一个颜色映射,规范化这个颜色映射,并创建一个对应于
df[“c”]
dataframe列中各个颜色的颜色字典。然后,我们再次使用
调色板
关键字将此词典提供给
swamplot

我们还需要删除自动生成但无用的图例,然后在绘图中创建一个新轴来放置颜色条

import pandas as pd

import matplotlib.pyplot as plt
import matplotlib.colorbar
import matplotlib.colors
import matplotlib.cm
from mpl_toolkits.axes_grid1 import make_axes_locatable

import seaborn as sns

# recreate the dataframe
a = [1,4,5,6,3,4,5,6]
c = [12,35,12,46,78,45,34,70]
key = [1,2,2,1,1,2,1,2]
key = ["{k}{a}".format(k=k, a={1:"st", 2:"nd"}[k]) for k in key]
df  =pd.DataFrame({"a":a, "c":c, "Key":key})

#Create a matplotlib colormap from the sns seagreen color palette
cmap    = sns.light_palette("seagreen", reverse=False, as_cmap=True )
# Normalize to the range of possible values from df["c"]
norm = matplotlib.colors.Normalize(vmin=df["c"].min(), vmax=df["c"].max())
# create a color dictionary (value in c : color from colormap) 
colors = {}
for cval in df["c"]:
    colors.update({cval : cmap(norm(cval))})

#create a figure
fig = plt.figure(figsize=(5,2.8))
#plot the swarmplot with the colors dictionary as palette
m = sns.swarmplot(x='Key', y = 'a',  hue="c", s=20, data = df, palette = colors)
# remove the legend, because we want to set a colorbar instead
plt.gca().legend_.remove()

## create colorbar ##
divider = make_axes_locatable(plt.gca())
ax_cb = divider.new_horizontal(size="5%", pad=0.05)
fig.add_axes(ax_cb)
cb1 = matplotlib.colorbar.ColorbarBase(ax_cb, cmap=cmap,
                                norm=norm,
                                orientation='vertical')
cb1.set_label('Some Units')
plt.show()

太棒了!!非常感谢,弄清楚色条的代码可能要花我三天时间@奥兹:如果你觉得这个答案有用的话,你可以投它一票。我投了,但我觉得我的声誉太低了。上面说,投票正在被记录,但没有显示。对不起,再次万分感谢!!简短问题:(为什么?)是否需要重新设置“关键”列的格式?我不明白它改变了什么。如果你有时间,一个很短的回应将是伟大的!再次感谢!!在代码中,
列包含类似于
'1st'
的字符串。因为我手头没有您的数据帧,所以我需要自己编写,这对我来说是最简单的方法。这实际上只是为了获得与您已有的数据帧相同的数据帧,因此无需担心。当然,如果需要,可以直接在列表中键入字符串。