Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not return matplotlib.Axes in python script #259

Open
AfterDream opened this issue Apr 8, 2024 · 3 comments
Open

Could not return matplotlib.Axes in python script #259

AfterDream opened this issue Apr 8, 2024 · 3 comments

Comments

@AfterDream
Copy link

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() 
@zqfang
Copy link
Owner

zqfang commented Apr 9, 2024

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

@wrb2012
Copy link

wrb2012 commented Sep 19, 2024

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

@zqfang
Copy link
Owner

zqfang commented Oct 1, 2024

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants