首页 > 解决方案 > 组合散点图和箱线图中同一行的颜色相同

问题描述

下面是我的数据和python代码。该代码将 5 组箱线图与基础数据(点)一起绘制。我希望属于相同原始数据的点具有相同的颜色(也就是说,来自同一主题的数据应该用所有 5 个组的相同颜色表示)。怎么可能做到这一点?

Group  Cups_Hab  Cups_STM       STM  Cups_LTM       LTM Control
0      1  0.125000 -0.041667 -0.175000 -0.108333  0.125000       y
1      2  0.000000  0.288703 -0.132075 -0.008333  0.408333       n
2      3 -0.100000  0.066667  0.050000  0.000000  0.158333       n
3      4 -0.141667 -0.008333  0.183333 -0.208333 -0.483333       n
4      5  0.075000 -0.525000 -0.683333 -0.374046  0.500000       n
5      6 -0.108333 -0.775000       NaN -0.416667       NaN       n
6      7 -0.150000  0.025000 -0.583333 -0.250000  0.548333       n
7      8 -0.100000 -0.096234 -0.516667  0.500000  0.708333       n
8      9  0.091667  0.200000 -0.416667 -0.063636  0.591667       n
9     10 -0.458333  0.250000 -0.066667  0.566667 -0.300000       y

import seaborn as sns
import pandas as pd
import csv
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

data = []

for el in df.drop("Group",axis=1).drop("Control",axis=1):
    for i,val in enumerate(df[el]):
        data.append([el,val,df.iloc[i].get('Control')])
        
newdf = (pd.DataFrame(data,columns=['Experimental Condition','Left preference','control']))

sns.stripplot(
    data=newdf,
    x='Experimental Condition',
    y='Left preference',
    hue='control',
    jitter=0,
    dodge=True
).axhline(linewidth=1, color='lightgrey',ls='--')

ax = sns.boxplot(
    data=newdf,
    x='Experimental Condition',
    y='Left preference',
    hue='control',
    palette="Set3",
    fliersize=0
)

handles, labels = ax.get_legend_handles_labels()
l = plt.legend(handles[0:2], ["Control", "Conditioned"])

ax.set_xticklabels(['Cups Habit.','Cups STM','Recall STM','Cups LTM','Recall LTM'])
ax.set_xlabel("Experimental Condition",fontsize=15)
ax.set_ylabel("Non-paired mouse preference",fontsize=15)


fig = ax.get_figure()
plt.tight_layout()
fig.tight_layout()
fig.savefig("Pilot3SidePreference.jpg",dpi=500)



 

标签: pythonplotseaborn

解决方案


推荐阅读