GSEApy icon indicating copy to clipboard operation
GSEApy copied to clipboard

Could not return matplotlib.Axes in python script

Open AfterDream opened this issue 10 months ago • 3 comments

Setup

I am reporting a problem with gseapy == 1.1.2, Python == 3.8.19, and operatingsystem == Ubuntu 20

My code as follows, the goal is to have gseapy complete the enrichment of each cluster in the for loop and save the resulting image to the local, I tested the feasibility in jupyter, but when executed in a standalone script, only a blank canvas was generated,unable to obtain matplotlib.Axes object, ignored the ofname parameter according to the document

import gseapy as gp
from gseapy import barplot, dotplot

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Agg')

for cluster in adata.obs['cluster'].value_counts().index.values:
    glist = sc.get.rank_genes_groups_df(adata, group=cluster,key ='wilcoxon', log2fc_min=1, pval_cutoff=0.05)['names'].squeeze().str.strip().tolist()
    print(cluster + "------------"+ str(len(glist)))
    if len(glist) > 10:
        go_enrich_res = gp.enrichr(gene_list=glist,
                            organism=species,
                            gene_sets=['GO_Biological_Process_2023','GO_Cellular_Component_2023','GO_Molecular_Function_2023'],  #
                            no_plot = True,
                            cutoff = 0.05)
        go_df = go_enrich_res.results
        if len( go_df[go_df["Adjusted P-value"] < 0.05] ) >0:
            ax = gp.barplot(go_df, figsize=(8,12),
                            group ='Gene_set',
                            column="Adjusted P-value",
                            title ="GO",
                            top_term = 10,
                            no_plot= True,
                            show=False,
                            color = ['b','r','y'])
            plt.savefig(fig_outdir+'05_'+cluster+'_marker_genes_GO_enrichment.png', bbox_inches="tight")
            plt.close() 

AfterDream avatar Apr 08 '24 07:04 AfterDream

You can set ofname to save your file in barplot

By default, plt.savefig will save figure in interactively mode. e.g. jupyter

In script mode, gseapy turns off the matplotlib's interactive GUI to avoid opening too many windows

zqfang avatar Apr 09 '24 19:04 zqfang

but I want to display pyplot in web page. right now I can only use tempfile to store result and use base64 to transfer to browser.

direct use gseapy.barplot without ofile only result in figure with bare x-y axis , in python network framework

wrb2012 avatar Sep 19 '24 04:09 wrb2012

I'm sorry I got back to you so late.

The reason you can't see anything because you use matplotlib.figure.Figure Object without GUI backend. This is designed for GSEApy in script mode.

To work around this issue, ref to here:

ax = gp.barplot(go_df, figsize=(8,12),
                group ='Gene_set',
                column="Adjusted P-value",
                title ="GO",
                top_term = 10,
                no_plot= True,
                show=False,
                color = ['b','r','y'])

## set interactive backend manually
fig2 = plt.subplots()
canvas_manager = fig2.canvas.manager
canvas_manager.canvas.figure = ax.figure
ax.figure.set_canvas(canvas_manager.canvas)

plt.show()

zqfang avatar Oct 01 '24 20:10 zqfang