我正在为来自系列的数据创建条形图。但是,名称(x轴值)非常长。如果将它们旋转90度,则无法读取整个名称并获得图形的良好图像。45度也好不到哪里去。我正在寻找一种方法,用数字1-15标记x轴,然后用图例列出每个数字对应的名称
这是我到目前为止完成的函数,包括从更大的数据帧创建系列
def graph_average_expressions(TAD_matches, CAGE):
"""graphs the top 15 expression levels of each lncRNA"""
for i, row in TAD_matches.iterrows():
mask = (
CAGE['short_description'].isin(row['peak_ID'])
)#finds expression level for peaks in an lncRNA
average = CAGE[mask].iloc[:,8:].mean(axis=0).astype('float32').sort_values().tail(n=15)
#made a new df of the top 15 highest expression levels for all averaged groups
#a group is peaks belong to the same lncRNA
cell_type = list(average.index)
expression = list(average.values)
average_df = pd.DataFrame(
list(zip(cell_type, expression)),
columns=['cell_type','expression']
)
colors = sns.color_palette(
'husl',
n_colors=len(cell_type)
)
p = sns.barplot(
x=average_df.index,
y='expression',
data=average_df,
palette=colors
)
cmap = dict(zip(average_df.cell_type, colors))
patches = [Patch(color=v, label=k) for k, v in cmap.items()]
plt.legend(
handles=patches,
bbox_to_anchor=(1.04, 0.5),
loc='center left',
borderaxespad=0
)
plt.title('expression_levels_of_lncRNA_' + row['lncRNA_name'])
plt.xlabel('cell_type')
plt.ylabel('expression')
plt.show()
下面是我正在绘制的数据示例
CD14_monocytes_treated_with_Group_A_streptococci_donor2.CNhs13532 1.583428
Neutrophils_donor3.CNhs11905 1.832527
CD14_monocytes_treated_with_Trehalose_dimycolate_TDM_donor2.CNhs13483 1.858384
CD14_monocytes_treated_with_Candida_donor1.CNhs13473 1.873013
CD14_Monocytes_donor2.CNhs11954 2.041607
CD14_monocytes_treated_with_Candida_donor2.CNhs13488 2.112112
CD14_Monocytes_donor3.CNhs11997 2.195365
CD14_monocytes_treated_with_Group_A_streptococci_donor1.CNhs13469 2.974203
Eosinophils_donor3.CNhs12549 3.566822
CD14_monocytes_treated_with_lipopolysaccharide_donor1.CNhs13470 3.685389
CD14_monocytes_treated_with_Salmonella_donor1.CNhs13471 4.409062
CD14_monocytes_treated_with_Candida_donor3.CNhs13494 5.546789
CD14_monocytes_-_treated_with_Group_A_streptococci_donor3.CNhs13492 5.673991
Neutrophils_donor1.CNhs10862 8.352045
Neutrophils_donor2.CNhs11959 11.595509
设置数据帧
备选方案1A:Seaborn
hue
seaborn.barplot
并将hue
参数与'names'
一起使用husl
husl
调色板的其他选项,请访问^{备选方案1B:Seaborn
palette
palette
参数而不是hue
,将条直接放置在刻度上李>'names'
与颜色关联并创建图例。Patch
创建图例中的每个项目。(例如,与颜色和名称关联的矩形)李>备选案文2:^{}
'names'
与调色板关联,并使用Patch
创建图例李>tab20c
可复制数据帧
有点不同的路线。制作一个字符串将x值映射到名称,并将其添加到图形中
制作了我自己的数据框以供说明
制作情节
制作图例
添加图例作为文本艺术家,并调整绘图以适应它
通过获取并使用文本艺术家的大小和人物的大小,可以使其动态
相关问题 更多 >
编程相关推荐