我正在使用Seaborn的FacetGrid和Matplotlib的错误栏来生成在y轴上具有两个类别的图形。我想将这两个类别居中(使它们彼此靠近,而距边距更远)。
我试图改变身高/纵横比,但没有成功。我正在分享下面的代码。谢谢!
df <- read.table(text =
'"ID" "subregion" "mutation" "attribute"
"1" "TCGA-AN-A046" "OR4F5:E1:E1" 69767 1.00849961637455
"2" "TCGA-A2-A0CP" "SAMD11:E2:E2" 925952 1.00346517231111
"3" "TCGA-A8-A08H" "NOC2L:E5:E5" 956126 1.43669428919156
"4" "TCGA-GM-A2DM" "NOC2L:E4:E4" 956911 1.1679575001733
"5" "TCGA-GM-A2DM" "NOC2L:E4:E4" 956912 1.1679575001733
"6" "TCGA-D8-A1XM" "KLHL17:E3:E3" 961658 2.17848956802821
"7" "TCGA-BH-A18G" "KLHL17:E5:E5" 962441 48.0640560165975
"8" "TCGA-3C-AALI" "KLHL17:E8:E8" 963353 40.6525553849528
"9" "TCGA-AC-A62Y" "KLHL17:E9:E9" 964004 2.89875813313313
"10" "TCGA-AR-A2LE" "PLEKHN1:E1:E1" 966556 1.03540263019699
"11" "TCGA-E2-A14N" "PLEKHN1:E5:E5" 970728 21.8246585021196
"12" "TCGA-AO-A0J4" "PLEKHN1:E12:E12" 973506 1.24409284966302
"13" "TCGA-D8-A1J9" "HES4:E3:E3" 999551 1.24409284966302
"14" "TCGA-EW-A1PH" "ISG15:E2:E2" 1014276 72.4814235432147
"15" "TCGA-A2-A0T0" "AGRN:E2:E2" 1022338 21.8246585021196
"16" "TCGA-GM-A2DD" "AGRN:E3:E3" 1035303 1.06314569745364
"17" "TCGA-5L-AAT1" "AGRN:E4:E4" 1040690 1.24409284966302
"18" "TCGA-OL-A5RW" "AGRN:E8:E8" 1043314 2.20878819659627
"19" "TCGA-D8-A27M" "AGRN:E25:E25" 1049355 1.45844645372491
"20" "TCGA-AR-A1AI" "AGRN:E29:E29" 1050430 1.16479379564338
"21" "TCGA-5L-AAT0" "AGRN:E36:E36" 1055374 7.09932582548073
"22" "TCGA-5L-AAT0" "AGRN:E36:E36" 1055376 7.09932582548073
"23" "TCGA-C8-A8HP" "AGRN:E36:E36" 1055442 7.09932582548073
"24" "TCGA-A7-A4SD" "TTLL10:E13:E13" 1184971 1.24409284966302
"25" "TCGA-BH-A1F0" "SDF4:E4:E4" 1223283 1.46091816304331
"26" "TCGA-AO-A128" "SDF4:E4:E4" 1223330 1.46091816304331
"27" "TCGA-E9-A1R0" "SDF4:E2:E2" 1228592 3.86565576505924
"28" "TCGA-A2-A04P" "UBE2J2:E7:E7" 1255246 33.795587162655
"29" "TCGA-C8-A274" "UBE2J2:E7:E7" 1255342 33.795587162655
"30" "TCGA-5L-AAT1" "SCNN1D:E1:E1" 1281422 1.24409284966302
"31" "TCGA-AO-A128" "SCNN1D:E6:E6" 1287116 1.06314569745364
"32" "TCGA-E2-A15R" "SCNN1D:E7:E7" 1287596 2.89179279138711
"33" "TCGA-AC-A62V" "SCNN1D:E11:E11" 1290543 74.0747402078337
"34" "TCGA-BH-A18V" "ACAP3:E22:E22" 1294187 2.21398621447599', header = T)
答案 0 :(得分:0)
您离它很近
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
# replace label with actual number
cat = [1,2,1,1,1,2,2,1]
var=[3,5,4,6,7,5,6,3]
sd_var = [0.1,0.3,0.1,0.5,0.4,0.2,0.1,0.2]
df = pd.DataFrame({'cat' : cat, 'var' : var, 'sd_var' : sd_var})
g = sns.FacetGrid(data=df, hue="cat", aspect=1, height=6)
g.map(plt.errorbar, "cat", "var", "sd_var", marker="o",
linestyle='',markersize='6', capsize=4, elinewidth=2)
# place the ticks at center by widening the plot
plt.xlim((0, 3))
# fix ticks at the number encoding for each class
g.fig.axes[0].xaxis.set_ticks([1, 2])
# name the numbers
g.fig.axes[0].xaxis.set_ticklabels(['a', 'b'])
plt.show()
诀窍是利用x限制的相对长度和类的数字编码。