Scanpy Dotplot / Matplotlib Help
1
0
Entering edit mode
7 weeks ago
cthangav ▴ 110

Hello, New to python/matplotlib.. My scanpy dotplot axis labels are getting cut off. I'm looking through the documentation and adjusting the figsize parameter helps, but the plot gets quite spread out. Is there a way to just adjust the canvas size or font size? enter image description here

Here is my code:

import scanpy as sc
dp = sc.pl.dotplot(
    adata, 
    var_names = genes_for_dot_plot, 
    groupby = annotation, 
    layer = layer_to_plot,
    #title = title,
    #figsize = [2,2],
    cmap = color_map,
    swap_axes=True,
    return_fig=True
    )
dp.add_totals().style(dot_edge_color='grey', dot_edge_lw=0.5).show()
Matplotlib Dotplot Python Scanpy • 823 views
ADD COMMENT
0
Entering edit mode
6 weeks ago
Pratik ★ 1.1k

Hello,

I am always asking questions like this to ChatGPT. It's great because you can provide feedback/code/screenshots based on the results to improve your code. It's not perfect, there can be mistakes, but feedback helps it improve answers.

Here are some suggestions it gave me for your question:

import scanpy as sc
import matplotlib.pyplot as plt

# Generate the dotplot and return the figure
dp = sc.pl.dotplot(
    adata, 
    var_names=genes_for_dot_plot, 
    groupby=annotation, 
    layer=layer_to_plot,
    #title=title,
    #figsize=[2, 2],  # optional
    cmap=color_map,
    swap_axes=True,
    return_fig=True
)

# Add totals, style the plot, and show it
dp = dp.add_totals().style(dot_edge_color='grey', dot_edge_lw=0.5)
dp.show()

# --- Adjust canvas to prevent labels from being cut off ---
fig = plt.gcf()
fig.subplots_adjust(left=0.3, bottom=0.25, right=0.95, top=0.95)  # tweak as needed

# Optional: control font size of axis labels
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)

plt.show()

If you want a bigger canvas instead of just shifting labels:

fig.set_size_inches(6, 5)  # width x height in inches

and then finally saving the image could generate it differently:

fig.savefig("dotplot.png", dpi=300, bbox_inches='tight')
ADD COMMENT
0
Entering edit mode

Thank you for your response. Unfortunately that set up creates a second blank plot and does the adjustment on the blank plot. fig = plt.gcf() somehow isn't retrieving the dotplot. It is able to retrieve it when I remove the return_fig part and set show to False. But then I'm not sure how to include the add_totals() method

dp = sc.pl.dotplot(
    adata, 
    var_names = genes_for_dot_plot, 
    groupby = annotation, 
    layer = layer_to_plot,
    #title = title,
    #figsize = [2,2],
    cmap = color_map,
    swap_axes=True,
    show=False
    )

# Add totals (this doesn't work)
# dp = dp.add_totals().style(dot_edge_color='grey', dot_edge_lw=0.5)

# Now, get the figure and axes that were just created.
fig = plt.gcf()
ax = plt.gca()

# Apply your adjustments to this figure.
fig.subplots_adjust(bottom=0.4, left=0.2)

# Show the final adjusted plot.
plt.show()
ADD REPLY

Login before adding your answer.

Traffic: 3733 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6