Facetgrid 对每个图形进行格式化和排序

Facetgrid Formatting and sorting each graph

下面是我为运行 facetgrid 图创建的代码。如您所见,merged1 是第一个数据帧,而 merged2 是我正在迭代的第二个数据帧。我想完成两件事。

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

# initialize list of lists
data = [['tom', 5000, 200,5, 900 ], ['tom', 7000, 500,5, 900 ], ['nick', 7000,300,4 ,4000], ['nick', 8000,200,4 ,4000], ['juli',9000,300,2, 8000,], ['juli',15000,300,2, 8000,], ['TEST',10000,300,3,8000], ['TEST',8000,800,3,8000],['hang', 5000, 330,1.6,5000 ], ['hang', 1000, 330,5,5000 ]]
 
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Donor', 'Days-post-ARV','CD4', 'VL','Days Post-COLDATE'])

#creating two separate datframes
merged1=df.drop(columns=['CD4'])
merged2=df.drop(columns=['VL'])

#function for reference vertical line 
def vertical_mean_line(x, **kwargs):
    plt.axvline(x.mean(), **kwargs)  

#for the first dataframe    
g = sns.relplot(data=merged1, x='Days-post-ARV', y='VL', col='Donor',col_wrap=3, kind="line", height=4, aspect=1.5,
                color='b')
#for the vertical line
g.map(vertical_mean_line, 'Days Post-COLDATE',ls="--",color='g')

#for the second dataframe
for patid, ax in g.axes_dict.items():  # axes_dict is new in seaborn 0.11.2
    ax1 = ax.twinx()
    sns.lineplot(data=merged2[merged2['Donor'] == patid], x='Days-post-ARV', y='CD4', color='r', ax=ax1)
    ax1.set_ylim(0,1200)

g.add_legend()   
g.tight_layout()

  1. 图表排序的最佳方式是什么?我想调换第二张和第三张图的顺序。
  2. 绘图时,每个图形之间的 Y 轴被合并,只能看到每个图形之间的 'CD4',但应该显示第一个 Y 轴标签 'VL'。我该怎么做?

嗯,测试数据和原测试代码给出:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# initialize list of lists
data = [['tom', 5000, 200, 5, 900], ['tom', 7000, 500, 5, 900], ['nick', 7000, 300, 4, 4000], ['nick', 8000, 200, 4, 4000], ['juli', 9000, 300, 2, 8000, ], ['juli', 15000, 300, 2, 8000, ], ['TEST', 10000, 300, 3, 8000], ['TEST', 8000, 800, 3, 8000], ['hang', 5000, 330, 1.6, 5000], ['hang', 1000, 330, 5, 5000]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Donor', 'Days-post-ARV', 'CD4', 'VL', 'Days Post-COLDATE'])

# creating two separate datframes
merged1 = df
merged2 = df

# function for reference vertical line
def vertical_mean_line(x, **kwargs):
     plt.axvline(merged1[x].mean(), **kwargs)

# for the first dataframe
g = sns.relplot(data=merged1, x='Days-post-ARV', y='VL', col='Donor', col_wrap=3, kind="line", height=4, aspect=1.5,
                color='b')
# for the vertical line
g.map(vertical_mean_line, x='Days-post-ARV', ls="--", color='g')

# for the second dataframe
for patid, ax in g.axes_dict.items():  # axes_dict is new in seaborn 0.11.2
     ax1 = ax.twinx()
     sns.lineplot(data=merged2[merged2['Donor'] == patid], x='Days-post-ARV', y='CD4', color='r', ax=ax1)
     ax1.set_ylim(0, 1200)
g.add_legend()
g.tight_layout()
plt.show()

您会注意到子图的顺序与捐赠者在数据框中出现的顺序相同:['tom', 'nick', 'juli', 'TEST', 'hang']

Seaborn 似乎不仅删除了左侧的 y 标签,还将其设置为不可见。您需要再次将其设置为可见。请注意,对 ax 的更改会影响左侧刻度线和左侧标签,而对 ax1 的更改会影响子图右侧的刻度线和标签。

更改代码以添加新的 col_order。并添加 ax.set_ylabel('VL', visible=True):


# for the first dataframe, NEW col_order
g = sns.relplot(data=merged1, x='Days-post-ARV', y='VL', col='Donor', col_wrap=3, kind="line", height=4, aspect=1.5,
                color='b', col_order=['tom', 'juli', 'nick', 'TEST', 'hang'])
# for the vertical line
g.map(vertical_mean_line, x='Days-post-ARV', ls="--", color='g')

# for the second dataframe
for patid, ax in g.axes_dict.items():  # axes_dict is new in seaborn 0.11.2
     ax1 = ax.twinx()
     sns.lineplot(data=merged2[merged2['Donor'] == patid], x='Days-post-ARV', y='CD4', color='r', ax=ax1)
     ax1.set_ylim(0, 1200)
     ax.set_ylabel('VL', visible=True) # ADDED

g.add_legend()
g.tight_layout()
plt.show()