试图将6个小提琴子图(使用seaborn制成)(格式化为3行2列)导出到一页PDF

问题描述 投票:0回答:1

我需要将通过seaborn通过python制作的6个小提琴子图导出到一页PDF中。它们需要格式化为3行x 2列。现在,我的代码正在生成包含6个空白图的单页PDF,并在控制台中显示此空白图网格以及我的6个小提琴子图(我需要以3x2网格格式显示)。我需要修复代码以使小提琴图正确导出为PDF。

data = pd.read_csv(os.path.join(input_folder, input_file))


x = "Subregion"
hue = "Mutation"
col = "Subregion"
kind = "violin"
data = M1
title_name = "M1"



fig, ([ax1, ax2], [ax3, ax4], [ax5, ax6]) = plt.subplots(nrows=3, ncols=2,figsize = (6,6))
fig.subplots_adjust(hspace=0.4, wspace=0.4)
ax1 = sns.catplot(x = x, y = "Area_mm", hue = hue, col = None, kind = kind, data = data, legend = False)
ax1.set_ylabels("Area (mm^2)")
ax2 = sns.catplot(x = x, y = "DAPI_count", hue = hue, col = None, kind = kind, data = data, legend = False)
ax2.set_ylabels("DAPI Cell Count")
ax3 = sns.catplot(x = x, y = "SST_count", hue = hue, col = None, kind = kind, data = data, legend = False)
ax3.set_ylabels("SST Cell Count")
ax4 = sns.catplot(x = x, y = "DAPI_per_area", hue = hue, col = None, kind = kind, data = data, legend = False)
ax4.set_ylabels("DAPI Cell Density (DAPI/mm^2)")
ax5 = sns.catplot(x = x, y = "SST_per_area", hue = hue, col = None, kind = kind, data = data, legend = False)
ax5.set_ylabels("SST Cell Density (SST/mm^2)")
ax6 = sns.catplot(x = x, y = "SST_per_DAPI", hue = hue, col = None, kind = kind, data = data, legend = False)
ax6.set_ylabels("SST Cell Density (% SST/DAPI cells)")

fig.savefig(os.path.join(output_folder, title_name + '.pdf'))
seaborn export-to-pdf violin-plot
1个回答
0
投票

问题可能是在列表中指定了斧头。。不是很确定那是如何工作的。您总是可以展平轴,并遍历标签和y轴值进行绘制,例如:

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

x = "Subregion"
hue = "Mutation"
kind = "violin"
title_name = "M1"

M1 = pd.DataFrame(np.random.normal(0,1,(100,6)),
                  columns=["Area_mm","DAPI_count","SST_count","DAPI_per_area","SST_per_area","SST_per_DAPI"])
M1['x'] = np.random.choice(['p','q','r'],100)
M1["Mutation"] = np.random.choice(['A','B'],100)

VAR = M1.columns[:6]
YL = ["Area (mm^2)","DAPI Cell Count","SST Cell Count","DAPI Cell Density (DAPI/mm^2)","SST Cell Density (SST/mm^2)","SST Cell Density (% SST/DAPI cells)"]

fig, axs = plt.subplots(3, 2,figsize = (8,8))
fig.subplots_adjust(hspace=0.4, wspace=0.4)
axs = axs.reshape(-1)
for k in range(len(VAR)):
    sns.violinplot(x = "x", y = VAR[k], hue = hue, col = None, 
                kind = kind, data = M1,ax=axs[k])
    axs[k].set_ylabel(YL[k],fontsize=8)
    axs[k].legend_.remove()
axs[-1].legend(loc='upper right', ncol=1,bbox_to_anchor=(1.5,1.5))
plt.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.