首页 > 解决方案 > 多种线型的 Seaborn Lineplot 限制的替代方案?

问题描述

我有一些纵向测试数据,我想检查这些数据的总体趋势。我的数据是这样设置的:

import seaborn as sns
import matplotlib.pyplot as plt

test_data = pd.read_csv('./Files Used to Generate Graphs/test_data.csv',header = 0)
test_data

在此处输入图像描述

我想绘制这些数据的方式是让每个捐赠者都有他/她自己的纵向数据线,但是根据捐赠者的性别为每条线着色,如下所示:

test_plt = sns.lineplot(x = 'Timepoint',y = 'Prevalence',
                   hue = 'Gender',
                   data = test_data,
                   style = 'Donor',
                   palette = dict(Male = 'red',
                                 Female = 'blue'))

for line in test_plt.lines:
    line.set_linestyle("-")
ax = plt.gca()
legend = ax.legend()
legend.set_visible(False)
plt.figure()

在此处输入图像描述

然而,seaborn 的 lineplot样式参数似乎被限制为 6 种类型。如果我尝试在我的数据中添加另一个捐助者并绘制它,我会得到:

append_df = pd.DataFrame(index = [12,13],
                    columns = ['Donor','Timepoint','Gender','Prevalence'])
append_df['Donor'] = 7
append_df['Gender'] = 'Female'
append_df.loc[12,'Timepoint'] = 1945
append_df.loc[13,'Timepoint'] = 1948
append_df.loc[12,'Prevalence'] = 18
append_df.loc[13,'Prevalence'] = 36
test_data = test_data.append(append_df)
test_data

在此处输入图像描述

test_plt = sns.lineplot(x = 'Timepoint',y = 'Prevalence',
                   hue = 'Gender',
                   data = test_data,
                   style = 'Donor',
                   palette = dict(Male = 'red',
                                 Female = 'blue'))

for line in test_plt.lines:
    line.set_linestyle("-")
ax = plt.gca()
legend = ax.legend()
legend.set_visible(False)
plt.figure()

在此处输入图像描述

那么有没有办法绕过线图的这个限制,还是我必须通过 Matplotlib 来完成这个如果是后者,Matplotlib 代码会是什么样子?

另外,有没有办法在 seaborn 图上生成图例,显示每个捐赠者的性别,而不是每个特定的捐赠者?

标签: pythonmatplotliblegendseabornline-plot

解决方案


推荐阅读