minoo
minoo

Reputation: 575

how can i add numeric scales to x and y axis of scanpy umap?

enter image description hereI am using the code below to make two population on my umap

adata_all.obs["cell_type_lvl0"] = adata_all.X[:, adata_all.var["marker"] == 'CD45RA'] > 0.5

adata_all.obs["cell_type_lvl0"] = adata_all.obs["cell_type_lvl0"].map(
    {True: "CD45+", False: "CD45-"}
)

import matplotlib.pyplot as plt
import scanpy.plotting as sc_pl

# create a new figure with a specified size
fig = plt.figure(figsize=(8, 8))

# plot UMAP with color
sc_pl.umap(adata_all, color='cell_type_lvl0', ax=True)

# set axis labels and tick labels
plt.xlabel('UMAP 1', fontsize=12)
plt.ylabel('UMAP 2', fontsize=12)
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)

# show the figure
plt.show()

he code on top shows me an empty umap. but, The line sc_pl.umap(adata_all, color='cell_type_lvl0') shows me umap but without numeric x and y axis. how can I fix this? The code on top shows me an empty umap

Upvotes: -1

Views: 690

Answers (1)

SyntaxNavigator
SyntaxNavigator

Reputation: 523

Please try the following:

import matplotlib.pyplot as plt
import scanpy as sc

# Assuming adata_all is your preprocessed AnnData object

# Run UMAP (if not already done)
sc.tl.umap(adata_all)

adata_all.obs["cell_type_lvl0"] = adata_all.X[:, adata_all.var["marker"] == 'CD45RA'] > 0.5
adata_all.obs["cell_type_lvl0"] = adata_all.obs["cell_type_lvl0"].map({True: "CD45+", False: "CD45-"})

# Plot UMAP with color
sc.pl.umap(adata_all, color='cell_type_lvl0', show=False)

# Get the current axis and set axis labels and tick labels
ax = plt.gca()
ax.set_xlabel('UMAP 1', fontsize=12)
ax.set_ylabel('UMAP 2', fontsize=12)
ax.tick_params(axis='both', which='major', labelsize=10)

# Show the figure
plt.show()

Upvotes: 0

Related Questions