Seaborn swarmplot 标记颜色
Seaborn swarmplot marker colors
我将我的数据存储在以下 pandas 数据帧 df 中。
我正在使用以下命令来绘制叠加的框和群图。
hue_plot_params = {'data': df,'x': 'Genotype','y': 'centroid velocity','hue': 'Surface'}
sns.boxplot(**hue_plot_params)
sns.swarmplot(**hue_plot_params)
但是,还有一类 'Fly' 我也想在我的群图中绘制为不同的标记颜色。由于每列中的数据由多种 'Fly' 类型组成,因此我希望相应标记具有多种颜色,而不是蓝色和橙色。
(忽略重要性栏,那些是用另一个命令制作的)
有人可以帮我解决这个问题吗?
以下方法遍历 'Fly' 个类别。一个swarmplot
需要一次性画出来,但是一个stripplot
可以一个接一个地画。
'Surface'
仍用作 hue
以将它们绘制为“闪避”,但调色板两次使用苍蝇类别的颜色。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
sns.set()
np.random.seed(2022)
df = pd.DataFrame({'Genotype': np.repeat(['SN1_TNT', 'SN2_TNT', 'SN3_TNT', 'SN4_TNT'], 100),
'centroid velocity': 20 - np.random.normal(0.01, 1, 400).cumsum(),
'Surface': np.tile(np.repeat(['smooth', 'rough'], 50), 4),
'Fly': np.random.choice(['Fly1', 'Fly2', 'Fly3'], 400)})
hue_plot_params = {'x': 'Genotype', 'y': 'centroid velocity', 'hue': 'Surface', 'hue_order': ['smooth', 'rough']}
ax = sns.boxplot(data=df, **hue_plot_params)
handles, _ = ax.get_legend_handles_labels() # get the legend handles for the boxplots
fly_list = sorted(df['Fly'].unique())
for fly, color in zip(fly_list, sns.color_palette('pastel', len(fly_list))):
sns.stripplot(data=df[df['Fly'] == fly], **hue_plot_params, dodge=True,
palette=[color, color], label=fly, ax=ax)
strip_handles, _ = ax.get_legend_handles_labels()
strip_handles[-1].set_label(fly)
handles.append(strip_handles[-1])
ax.legend(handles=handles)
plt.show()
我将我的数据存储在以下 pandas 数据帧 df 中。
我正在使用以下命令来绘制叠加的框和群图。
hue_plot_params = {'data': df,'x': 'Genotype','y': 'centroid velocity','hue': 'Surface'}
sns.boxplot(**hue_plot_params)
sns.swarmplot(**hue_plot_params)
但是,还有一类 'Fly' 我也想在我的群图中绘制为不同的标记颜色。由于每列中的数据由多种 'Fly' 类型组成,因此我希望相应标记具有多种颜色,而不是蓝色和橙色。
(忽略重要性栏,那些是用另一个命令制作的)
有人可以帮我解决这个问题吗?
以下方法遍历 'Fly' 个类别。一个swarmplot
需要一次性画出来,但是一个stripplot
可以一个接一个地画。
'Surface'
仍用作 hue
以将它们绘制为“闪避”,但调色板两次使用苍蝇类别的颜色。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
sns.set()
np.random.seed(2022)
df = pd.DataFrame({'Genotype': np.repeat(['SN1_TNT', 'SN2_TNT', 'SN3_TNT', 'SN4_TNT'], 100),
'centroid velocity': 20 - np.random.normal(0.01, 1, 400).cumsum(),
'Surface': np.tile(np.repeat(['smooth', 'rough'], 50), 4),
'Fly': np.random.choice(['Fly1', 'Fly2', 'Fly3'], 400)})
hue_plot_params = {'x': 'Genotype', 'y': 'centroid velocity', 'hue': 'Surface', 'hue_order': ['smooth', 'rough']}
ax = sns.boxplot(data=df, **hue_plot_params)
handles, _ = ax.get_legend_handles_labels() # get the legend handles for the boxplots
fly_list = sorted(df['Fly'].unique())
for fly, color in zip(fly_list, sns.color_palette('pastel', len(fly_list))):
sns.stripplot(data=df[df['Fly'] == fly], **hue_plot_params, dodge=True,
palette=[color, color], label=fly, ax=ax)
strip_handles, _ = ax.get_legend_handles_labels()
strip_handles[-1].set_label(fly)
handles.append(strip_handles[-1])
ax.legend(handles=handles)
plt.show()